Objective c 具有空参数的RestKit POST对象

Objective c 具有空参数的RestKit POST对象,objective-c,post,http-post,restkit,restkit-0.20,Objective C,Post,Http Post,Restkit,Restkit 0.20,如果您对RESTKit有深入的了解,我们将非常感谢您的帮助。多谢各位 我尝试将一个核心数据实体发布到服务器,它连接良好,但服务器返回一个响应,告诉我所有参数都为NULL。我知道这项服务是有效的,因为我用AFNetworking打了同样的服务电话。不幸的是,我不能自由访问服务器 这就是我所做的。我创建了一个核心数据实体 PDRepresentative *representative = [NSEntityDescription insertNewObjectForEntityForName:@"

如果您对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加法器。它说了一些类似的话,它期望得到请求对象类,但得到了响应对象类

还有什么要注意的吗

如果我在收到请求后运行此命令

// 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

下面是使用网络代码的代码

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";
    };
}
AFHTTPClient*client=[RKObjectManager sharedManager].HTTPClient;
NSDictionary*参数=@{
@“RepId”:@“123”,
@“第一”:“杰克”,
@"中":@"Q",,
@"最后":@"公众",,
@“Address1”:@“示例字符串5”,
@“Address2”:@“示例字符串6”,
@“城市”:@“示例字符串7”,
@“状态”:@“样本字符串8”,
@“Zip”:@“示例字符串9”,
@“国家”:@“样本字符串10”,
@“公司”:@“样本串11”,
@“电话”:@“示例字符串12”,
@“PhonePersonal”:@“示例字符串13”,
@“传真”:@“样本字符串14”,
@“电子邮件”:@“示例字符串15”,
@“专业”:@“样本串16”,
@“EventId”:@“EventId”
};
client.parameterEncoding=afjson参数编码;
NSURLRequest*request=[客户端requestWithMethod:@“POST”路径:@“PostAddOrUpdateRep”参数:参数];
AFHTTPRequestOperation*operation=[客户端HTTPRequestOperationWithRequest:request
成功:^(AFHTTPRequestOperation*操作,id响应对象){
跟踪日志(@“%s\nResponseObject%@”,函数,响应对象);
}
失败:^(AFHTTPRequestOperation*操作,NSError*错误){
跟踪日志(@“操作%@”,操作);
跟踪日志(@“错误%@”,错误);
}];
[客户端排队HttpRequestOperation:operation];
//检查http正文
n错误*错误=nil;
NSDictionary*test=[NSJSONSerialization JSONObjectWithData:request.HTTPBody
选项:针织品
错误:&错误];
NSLog(@“http正文%@”,测试);
NSLog(@“错误%@”,错误);
控制台输出
2014-01-07 20:24:16.370 xctest[65939:303]http正文{
Address1=“示例字符串5”;
Address2=“示例字符串6”;
City=“示例字符串7”;
Company=“示例字符串11”;
Country=“示例字符串10”;
Email=“示例字符串15”;
EventId=EventId;
Fax=“示例字符串14”;
第一个=插孔;
最后=公共;
中间=Q;
Phone=“示例字符串12”;
PhonePersonal=“示例字符串13”;
RepId=123;
Speciality=“样本串16”;
State=“示例字符串8”;
Zip=“示例字符串9”;
}
2014-01-07 20:24:16.371 xctest[65939:303]错误(空)
2014-01-07 20:24:16.373 xctest[65939:303]I restkit.网络:RKObjectRequestOperation.m:180 POST'http://192.168.xxx.xxx/PostAddOrUpdateRep/1'
2014-01-07 20:24:18.755 xctest[65939:303]I restkit.网络: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]\uuuuuuu20-[PMGRepTests test2]\ublock\u0
响应对象{
Ex=“”;
Message=“”;
成功=1;
TransactionId=1;
值={
Address1=“示例字符串5”;
Address2=“示例字符串6”;
City=“示例字符串7”;
Company=“示例字符串11”;
Country=“示例字符串10”;
Email=“示例字符串15”;
EventId=EventId;
Fax=“示例字符串14”;
第一个=插孔;
最后=公共;
中间=Q;
Phone=“示例字符串12”;
PhonePersonal=“示例字符串13”;
RepId=123;
Speciality=“样本串16”;
State=“示例字符串8”;
Zip=“示例字符串9”;
};
}

在RestKit中,您似乎没有将请求序列化类型设置为JSON(默认为表单URL编码)

您也不希望在请求描述符上设置
rootKeyPath:@“postaddorupdateep”
,它应该设置为
nil