使用 RestKit 发送 POST 不会返回 JSON 内容
Posted
技术标签:
【中文标题】使用 RestKit 发送 POST 不会返回 JSON 内容【英文标题】:Sending POST with RestKit returns no JSON content 【发布时间】:2013-07-15 17:00:23 【问题描述】:所以,我是第一次发送 POST 请求。我是映射类,正如我从文档中想到的那样,它会以这种方式工作:
初始化 RK:
- (void)initRK
if(!manager)
manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:BASE_CONTEXT_URL]];
if (!reqMapping)
reqMapping = [RKObjectMapping requestMapping];
POST 方法:
// Configure a request mapping for our Article class. We want to send back title, body, and publicationDate
RKObjectMapping* deviceRequestMapping = [RKObjectMapping mappingForClass:[DeviceDTO class]];
[deviceRequestMapping addAttributeMappingsFromArray:@[ @"model", @"name", @"systemName", @"systemVersion", @"devToken" ]];
RKObjectMapping* msRequestMapping = [RKObjectMapping mappingForClass:[MemberShipDTO class]];
[msRequestMapping addAttributeMappingsFromArray:@[ @"validSince", @"validTill" ]];
RKObjectMapping* countryRequestMapping = [RKObjectMapping mappingForClass:[CountryDTO class]];
[countryRequestMapping addAttributeMappingsFromArray:@[ @"idNumberDTO", @"iso2DTO", @"short_nameDTO", @"calling_codeDTO" ]];
RKObjectMapping* userRequestMapping = [RKObjectMapping requestMapping];
[userRequestMapping addAttributeMappingsFromArray:@[ @"displayName", @"phoneNumber", @"status", @"userID" ]];
[userRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"device" withMapping:deviceRequestMapping]];
[userRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"memberShip" withMapping:msRequestMapping]];
[userRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"country" withMapping:countryRequestMapping]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMapping objectClass:[User class] rootKeyPath:@"user"];
//Create Objects
UserDTO *user = [[UserDTO alloc]init];
user.displayName = userDTO.displayName;
user.phoneNumber = userDTO.phoneNumber;
user.status = userDTO.status;
user.userID = userDTO.userID;
user.country = userDTO.country;
DeviceDTO *device = [[DeviceDTO alloc]init];
device.name = devDTO.name;
device.systemName = devDTO.systemName;
device.systemVersion = devDTO.systemVersion;
device.model = devDTO.model;
device.devToken = [[NSUserDefaults standardUserDefaults]objectForKey:PUSHTOKEN_USER_DEFAULTS_KEY];
user.deviceInfo = device;
MemberShipDTO *ms = [[MemberShipDTO alloc]init];
ms.validSince = [NSDate date];
ms.validTill = [[UtilitieHandler new] getDateByAdd:+1 :0 :0 :0];
user.memberShipDetails = ms;
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"application/json"];
[[RKObjectManager sharedManager] setRequestSerializationMIMEType:RKMIMETypeJSON];
[[RKObjectManager sharedManager] setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];
[[RKObjectManager sharedManager] postObject:user path:@"user/integrate" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
failure:^(RKObjectRequestOperation *operation, NSError *error)
RKLogError(@"Operation failed with error: %@", error);
];
所以我尝试了不同的方法,在我使用wireshark 捕获请求后,它返回没有内容发送。这意味着映射工作不正确。我尝试了很多,但没有任何帮助。任何建议都会很棒!
这里是捕获的数据包:
POST /WAZZUUPWS/rest/service/user/integrate HTTP/1.1
Host: 192.168.2.115:8080
Accept-Encoding: gzip, deflate
Accept: application/json
Content-Length: 0
Connection: keep-alive
Accept-Language: de;q=1, en;q=0.9, fr;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5
User-Agent: WAZZUUP!/1.0 (iPhone; ios 6.1.4; Scale/2.00)
【问题讨论】:
requestDescriptor
似乎没有链接到 UserDTO
类。是错字吗?
老兄,我刚刚看到了。我会在我回家的时候检查一下。我希望这就是问题所在!写一个答案,如果它是真的,我可以给予学分!
【参考方案1】:
这可能只是您的问题中的一个错字,但 requestDescriptor
似乎没有链接到 UserDTO 类。
【讨论】:
在打错字后遇到了一些问题,但这确实是错字!非常喜欢【参考方案2】:您似乎还没有了解 Core Data 对象。使用 Core Data 持久化的对象是 NSManagedObject 的子类,必须以不同的方式创建。在此链接上进一步阅读: http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/Articles/cdCreateMOs.html
至于目前的问题,你得改用这个:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserDTO" inManagedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext];
UserDTO *user = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
但是,如果 UserDTO 是 NSObject 的子类,则需要更改为 NSManagedObject。
我的工作流程是这样的——创建核心数据模型并使用 mogenerator 自动生成 NSManagedObject 类定义。在此处阅读更多信息:http://raptureinvenice.com/getting-started-with-mogenerator/
【讨论】:
我的问题与 Coredata 无关,抱歉。以上是关于使用 RestKit 发送 POST 不会返回 JSON 内容的主要内容,如果未能解决你的问题,请参考以下文章
RestKit POST 发送 JSON 不包含映射路径/类名