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\"}";
你現在可以看看我更新的代碼嗎?我仍然與我的發佈請求打架:/ – sonoDamiano
你是對的,這是服務器端的問題。再次感謝我的RestKit大師:) – sonoDamiano