【发布时间】:2014-01-08 16:29:10
【问题描述】:
如果您对 RESTKit 有深入的了解,我们将不胜感激您的意见。谢谢。
我尝试向服务器发布一个核心数据实体,它连接正常,但服务器给我一个响应,告诉我所有参数都是 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";
};
}
【问题讨论】:
标签: objective-c post http-post restkit restkit-0.20