您好我试图使用Rest Kit将NSManagedObject发布到服务器。有人可以看看,告诉我我做错了什么吗?
这是我的post请求的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
}
}
我的帖子方法如下所示:
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 =@"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 :(得分:0)
您需要针对NSDictionary
创建请求描述符,然后不使用HTTPClient
来操作帖子,只需使用对象管理器(因为客户端对请求或响应描述符一无所知)。 / p>
编辑后,您的服务器发出错误 - 请检查服务器日志。您的代码现在可以通过嵌套手表和接收手表并通过外键映射连接它们来发送佩戴者。您需要确切地检查Charles发送的内容与服务器期望的内容。