2014-06-05 99 views
1

嗨我想使用Rest Kit发布NSManagedObject到服务器。有人可以看看并告诉我我做错了什么吗?RestKit发布请求问题

这是我的JSON结构,岗位要求,我测试用的RubyMine REST客户端,并妥善张贴对象:

{ 
     "name":"test name", 
     "mobile":"22334455", 
     "dob":"1989-12-12", 
     "medications":"test", 
     "medical_conditions":"", 
     "watch": 
     { 
      "phonewatchno":"123789", 
      "latitude":-33.99, 
      "longitude":151.11, 
      "gmaps":null, 
      "battery_life":null, 
      "last_update_time":null, 
      "location":"", 
      "short_imei":null, 
      "rssi":null, 
      "charging_status":null, 
      "voltage":null, 
      "network":null, 
      "altitude":null, 
      "ip_addr":null, 
      "button_press_time":null, 
      "last_keep_alive":null 
     } 
    } 

我POST方法看起来像以下之一:

id params = @{@"name": @"Tinder", 
       @"mobile":@"20934920", 
       @"dob":@"1989-12-12", 
       @"medications":@"pills", 
       @"medical_conditions":@"lkas", 
       @"watch": 
        @{ 
         @"phonewatchno":@"239847239", 
         @"latitude":@"-33.99", 
         @"longitude":@"151.11", 
         @"gmaps":@"", 
         @"battery_life":@"", 
         @"last_update_time":@"", 
         @"location":@"", 
         @"short_imei":@"", 
         @"rssi":@"", 
         @"charging_status":@"", 
         @"voltage":@"", 
         @"network":@"", 
         @"altitude":@"", 
         @"ip_addr":@"", 
         @"button_press_time":@"", 
         @"last_keep_alive":@"" 

        } 

       }; 


    RKResponseDescriptor *responseDescr= [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider myMapping] method:RKRequestMethodPOST pathPattern:@"/myPath.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

    [[RKObjectManager sharedManager]addResponseDescriptor:responseDescr]; 
    [[RKObjectManager sharedManager]setRequestSerializationMIMEType:RKMIMETypeJSON]; 
    [[[RKObjectManager sharedManager]HTTPClient]postPath:@"/myPath.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { 

    NSLog(@"Mapped object %@",responseObject); 
    [self dismissViewControllerAnimated:YES completion:nil]; 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    { 
    NSLog(@"An error occuered: %@",error); 
}]; 

} 

谢谢提前

编辑1:

NSEntityDescription *watchEntityDesc = [NSEntityDescription entityForName:@"Watches" inManagedObjectContext:[[RKObjectManager sharedManager]managedObjectStore].mainQueueManagedObjectContext]; 

Watches *watch = [[Watches alloc]initWithEntity:watchEntityDesc insertIntoManagedObjectContext:[[RKObjectManager sharedManager]managedObjectStore].mainQueueManagedObjectContext]; 
watch.phonewatchno = [NSNumber numberWithInt:124512]; 
watch.latitude = [NSNumber numberWithDouble:-33.99]; 
watch.longitude = [NSNumber numberWithDouble:151.11]; 
watch.location = @"test location"; 
watch.gmaps = [NSNumber numberWithInt:12]; 
watch.battery_life = [NSNumber numberWithInt:78]; 
watch.last_update_time = [NSDate date]; 
watch.last_keep_alive = [NSDate date]; 
watch.short_imei = [NSNumber numberWithInt:1233]; 
watch.rssi = [NSNumber numberWithInt:2]; 
watch.charging_status = [NSNumber numberWithInt:1]; 
watch.voltage = [NSNumber numberWithInt:200]; 
watch.network = @"WIFI"; 
watch.altitude = [NSNumber numberWithInt:1]; 
watch.ip_addr = @"196.12.12.04"; 
watch.button_press_time = [NSNumber numberWithInt:4]; 

NSEntityDescription *wearersEntityDesc = [NSEntityDescription entityForName:@"Wearers" inManagedObjectContext:[[RKObjectManager sharedManager]managedObjectStore].mainQueueManagedObjectContext]; 

Wearers *wearer = [[Wearers alloc]initWithEntity:wearersEntityDesc insertIntoManagedObjectContext:[[RKObjectManager sharedManager]managedObjectStore].mainQueueManagedObjectContext]; 
wearer.name [email protected]"test"; 
wearer.mobile = @"109283190"; 
wearer.medical_conditions = @"test"; 
wearer.medications = @"test"; 
wearer.dob = [NSDate date]; 
wearer.watches = [NSSet setWithObject:watch]; 

RKEntityMapping *watchesMapping = [RKEntityMapping mappingForEntityForName:@"Watches" inManagedObjectStore:[[EdisseDateModel sharedDataModel]objectStore]]; 
[watchesMapping addAttributeMappingsFromDictionary:@{ 

               @"id": @"watch_id", 
               @"altitude":@"altitude", 
               @"battery_life":@"battery_life", 
               @"button_press_time":@"button_press_time", 
               @"charging_status":@"charging_status", 
               @"gmaps":@"gmaps", 
               @"ip_addr":@"ip_addr", 
               @"last_keep_alive":@"last_keep_alive", 
               @"last_update_time":@"last_update_time", 
               @"latitude":@"latitude", 
               @"longitude":@"longitude", 
               @"location":@"location", 
               @"network":@"network", 
               @"phonewatchno":@"phonewatchno", 
               @"rssi":@"rssi", 
               @"short_imei":@"short_imei", 
               @"updated_at":@"updated_at", 
               @"voltage":@"voltage", 
               @"wearer_id":@"wearer_id", 
               @"updated_at":@"updated_at", 



               } 


]; 

[watchesMapping addConnectionForRelationship:@"wearer" connectedBy:@{ 
                   @"wearer_id":@"wearer_id" 
                   }]; 
[watchesMapping setIdentificationAttributes:@[@"watch_id"]]; 


RKResponseDescriptor *responseDescr = [RKResponseDescriptor responseDescriptorWithMapping:watchesMapping method:RKRequestMethodPOST pathPattern:@"/watches.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 



[[RKObjectManager sharedManager]addResponseDescriptor:responseDescr]; 


RKEntityMapping *wearersMapping = [RKEntityMapping mappingForEntityForName:@"Wearers" inManagedObjectStore:[[EdisseDateModel sharedDataModel] objectStore]]; 

[wearersMapping addAttributeMappingsFromDictionary:@{ 
               @"id":@"wearer_id", 
               @"at_risk": @"at_risk", 
               @"created_at": @"created_at", 
               @"dob": @"dob", 
               @"geo_radius": @"geo_radius", 
               @"medical_conditions":@"medical_conditions", 
               @"medications":@"medications", 
               @"mobile":@"mobile", 
               @"name":@"name", 
               @"status":@"status", 
               @"updated_at":@"updated_at", 
               @"wearer_photo_content_type":@"wearer_photo_content_type", 
               @"wearer_photo_file_size":@"wearer_photo_file_size", 
               @"wearer_photo_file_name":@"wearer_photo_file_name", 

               } 
]; 
    wearersMapping.identificationAttributes = @[@"wearer_id"]; 

[wearersMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"watch" toKeyPath:@"watches" withMapping:watchesMapping]]; 




RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:wearersMapping method:RKRequestMethodPOST pathPattern:@"/wearers.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 



RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[wearersMapping inverseMapping] objectClass:[Wearers class] rootKeyPath:nil method:RKRequestMethodPOST ]; 

[[RKObjectManager sharedManager]addResponseDescriptor:responseDescriptor]; 
[[RKObjectManager sharedManager]addRequestDescriptor:requestDescriptor]; 

[[RKObjectManager sharedManager]setRequestSerializationMIMEType:RKMIMETypeJSON]; 
[[RKObjectManager sharedManager]setAcceptHeaderWithMIMEType:@"application/json"]; 

[[RKObjectManager sharedManager]postObject:wearer path:@"/wearers.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 
{ 
    NSLog(@"Response : %@",mappingResult.array); 
} 
failure:^(RKObjectRequestOperation *operation, NSError *error) 
{ 
    NSLog(@"Error: %@",error.userInfo.description); 
}]; 

现在我正从失败块错误在我的Xcode控制台象下面这样:

Error: { 
AFNetworkingOperationFailingURLRequestErrorKey = "<NSMutableURLRequest: 0x178210760> { URL: https://www.test.com/wearers.json }"; 
AFNetworkingOperationFailingURLResponseErrorKey = "<NSHTTPURLResponse: 0x170421be0> { URL: https://www.test.com/wearers.json } { status code: 500, headers {\n Connection = \"keep-alive\";\n \"Content-Length\" = 48;\n \"Content-Type\" = \"application/json; charset=utf-8\";\n Date = \"Fri, 06 Jun 2014 03:43:59 GMT\";\n Server = \"nginx/1.6.0 + Phusion Passenger 4.0.42\";\n Status = \"500 Internal Server Error\";\n \"X-Powered-By\" = \"Phusion Passenger 4.0.42\";\n \"X-Request-Id\" = \"6ce273e7-4130-47c3-91de-8020db49426a\";\n \"X-Runtime\" = \"0.011948\";\n} }"; 
NSErrorFailingURLKey = "https://www.test.com/wearers.json"; 
NSLocalizedDescription = "Expected status code in (200-299), got 500"; 
NSLocalizedRecoverySuggestion = "{\"status\":\"500\",\"error\":\"Internal Server Error\"}"; 

回答

0

您需要对NSDictionary创建请求描述符,然后不使用HTTPClient行动后,只需要使用对象经理(因为客户对请求或响应描述符一无所知)。


在您编辑您的服务器后发生错误 - 请检查服务器日志。您的代码现在看起来不错,因为它向佩戴者发送嵌套手表并接收手表并通过外键映射连接手表。您需要检查Charles发送的与服务器期望的内容完全相同的内容。

+0

你现在可以看看我更新的代码吗?我仍然与我的发布请求打架:/ – sonoDamiano

+0

你是对的,这是服务器端的问题。再次感谢我的RestKit大师:) – sonoDamiano