如果您对RESTKit有深入了解,我们将非常感谢您的意见。谢谢。
我尝试将核心数据实体POST到服务器,它连接正常,但服务器给我回复一个响应告诉我所有参数都是NULL。我知道该服务有效,因为我使用AFNetworking进行了相同的服务调用。不幸的是,我没有免费访问服务器。
这就是我的所作所为。我创建了一个核心数据实体
PDRepresentative *representative = [NSEntityDescription insertNewObjectForEntityForName:@"PDRepresentative" inManagedObjectContext:context];
representative.address1 = @"address1";
representative.address2 = @"address2";
representative.city = @"city";
representative.city = @"city";
representative.company = @"company";
representative.country = @"country";
representative.email = @"email";
representative.fax = @"fax";
representative.firstName = @"TestFirst";
representative.lastName = @"lastName";
representative.middleName = @"middleName";
representative.phone = @"phone";
representative.phonePersonal = @"phonePersonal";
representative.repId = @"TEST_REP_ID";
representative.specialty = @"spec";
representative.state = @"state";
representative.zip = @"zip";
representative.event = event;
//Create the mapping
RKObjectMapping *postObjectMapping = [RKObjectMapping requestMapping];
[postObjectMapping addAttributeMappingsFromDictionary:@{@"repId":@"RepId",
@"firstName":@"First",
@"middleName": @"Middle",
@"lastName": @"Last",
@"address1": @"Address1",
@"address2": @"Address2",
@"city": @"City",
@"zip": @"Zip",
@"country": @"Country",
@"company": @"Company",
@"phone": @"Phone",
@"phonePersonal": @"PhonePersonal",
@"fax": @"Fax",
@"email": @"Email",
@"event.eventId": @"EventId",
@"specialty": @"Specialty"}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:postObjectMapping
objectClass:[PDRepresentative class]
rootKeyPath:@"PostAddOrUpdateRep"
method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptor];
//… assign the response descriptor
NSMutableURLRequest *request = [objectManager requestWithObject:representative method:RKRequestMethodPOST path:@"PostAddOrUpdateRep" parameters:nil];
RKObjectRequestOperation *operation = [objectManager objectRequestOperationWithRequest:request
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
NSLog(@"http body %@", test);
NSLog(@"error %@", error);
PDResponse *response = mappingResult.firstObject;
response = [response isKindOfClass:[PDResponse class]] ? response : nil;
TRACE_LOG(@"%@", response);
}
failure:^(RKObjectRequestOperation *operation, NSError *error){
NSLog(@"operation %@, error %@", operation, error);
}];
[objectManager enqueueObjectRequestOperation:operation];
我使用这种发布对象的方法而不是使用postObject:path:parameters:success:failure因为它将目标对象指定为与请求对象相同但这会引发错误,因为响应对象完全是不同于发送的内容。它会调用RKMapperOperation addError。它正在说一些事情,它期待请求对象类,但得到了响应对象类。
要注意的其他事项
如果我在收到请求后运行
// check the http body
NSDictionary *test = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
options:kNilOptions
error:&error];
NSLog(@"http body %@", test);
NSLog(@"error %@", error);
它给了我以下输出
2014-01-07 20:15:43.626 xctest[65449:303] http body (null)
2014-01-07 20:15:43.627 xctest[65449:303] error Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x2a49940 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
另外需要注意的是,我做了一些测试并发现http主体是从以下字典创建的
(lldb) po [objectParameters requestParameters]
{
"PostAddOrUpdateRep" = {
Address1 = address1;
Address2 = address2;
City = city;
Company = company;
Country = country;
Email = email;
EventId = EventId;
Fax = fax;
First = TestFirst;
Last = lastName;
Middle = middleName;
Phone = phone;
PhonePersonal = phonePersonal;
RepId = "TEST_REP_ID";
Specialty = spec;
Zip = zip;
};
}
这似乎不对。我继续改变了http主体,与AFNetworking创建的请求的HTTP主体相同,后者从NSJSONSerialization中正确反序列化,服务器仍然给我一个异常,告诉我所有参数都是NULL。
以下是工作AFNetworking代码的代码
AFHTTPClient *client = [RKObjectManager sharedManager].HTTPClient;
NSDictionary *parameters = @{
@"RepId": @"123",
@"First": @"Jack",
@"Middle": @"Q",
@"Last": @"Public",
@"Address1": @"sample string 5",
@"Address2": @"sample string 6",
@"City": @"sample string 7",
@"State": @"sample string 8",
@"Zip": @"sample string 9",
@"Country": @"sample string 10",
@"Company": @"sample string 11",
@"Phone": @"sample string 12",
@"PhonePersonal": @"sample string 13",
@"Fax": @"sample string 14",
@"Email": @"sample string 15",
@"Specialty": @"sample string 16",
@"EventId": @"EventId"
};
client.parameterEncoding = AFJSONParameterEncoding;
NSURLRequest *request = [client requestWithMethod:@"POST" path:@"PostAddOrUpdateRep" parameters:parameters];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
TRACE_LOG(@"%s \nResponseObject %@", __PRETTY_FUNCTION__, responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
TRACE_LOG(@"Operation %@", operation);
TRACE_LOG(@"Error %@", error);
}];
[client enqueueHTTPRequestOperation:operation];
// check the http body
NSError *error = nil;
NSDictionary *test = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
options:kNilOptions
error:&error];
NSLog(@"http body %@", test);
NSLog(@"error %@", error);
The console output
2014-01-07 20:24:16.370 xctest[65939:303] http body {
Address1 = "sample string 5";
Address2 = "sample string 6";
City = "sample string 7";
Company = "sample string 11";
Country = "sample string 10";
Email = "sample string 15";
EventId = EventId;
Fax = "sample string 14";
First = Jack;
Last = Public;
Middle = Q;
Phone = "sample string 12";
PhonePersonal = "sample string 13";
RepId = 123;
Specialty = "sample string 16";
State = "sample string 8";
Zip = "sample string 9";
}
2014-01-07 20:24:16.371 xctest[65939:303] error (null)
2014-01-07 20:24:16.373 xctest[65939:303] I restkit.network:RKObjectRequestOperation.m:180 POST 'http://192.168.xxx.xxx/PostAddOrUpdateRep/1'
2014-01-07 20:24:18.755 xctest[65939:303] I restkit.network:RKObjectRequestOperation.m:216 POST 'http://192.168.xxx.xxx/PostAddOrUpdateRep/1' (200 OK) [2.3812 s]
2014-01-07 20:24:24.473 xctest[65939:303] __20-[PMGRepTests test2]_block_invoke
ResponseObject {
Ex = "<null>";
Message = "";
Success = 1;
TransactionId = 1;
Value = {
Address1 = "sample string 5";
Address2 = "sample string 6";
City = "sample string 7";
Company = "sample string 11";
Country = "sample string 10";
Email = "sample string 15";
EventId = EventId;
Fax = "sample string 14";
First = Jack;
Last = Public;
Middle = Q;
Phone = "sample string 12";
PhonePersonal = "sample string 13";
RepId = 123;
Specialty = "sample string 16";
State = "sample string 8";
Zip = "sample string 9";
};
}
答案 0 :(得分:1)
在RestKit中,您似乎没有将请求序列化类型设置为JSON(默认为表单URL编码)。
您的请求描述符上也不需要rootKeyPath:@"PostAddOrUpdateRep"
,它应设置为nil
。