gpt4 book ai didi

ios - 使用 RestKit 0.20.3 映射 json

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:46 25 4
gpt4 key购买 nike

我正在尝试通过 RestKit 0.20.3 映射对象,但我知道这些日志已经好几天了:

2013-08-12 18:32:08.158 MyAppIphone[848:5703] E restkit.network:RKResponseMapperOperation.m:304 Failed to parse response data: Loaded an unprocessable response (200) with content type 'application/json'

2013-08-12 18:32:08.174 MyAppIphone[848:5703] E restkit.network:RKObjectRequestOperation.m:238 POST 'myUrl' (200 OK / 0 objects)

[request=0.1305s mapping=0.0000s total=5.6390s]:
error=Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (200) with content type 'application/json'"

UserInfo=0x1ed5a500 {NSErrorFailingURLKey=myUrl, NSUnderlyingError=0x1ed5b240 "The operation couldn’t be completed. (Cocoa error 3840.)", NSLocalizedDescription=Loaded an unprocessable response (200) with content type 'application/json'}

response.body={"my json content"}

这是 MyData 类:

#import <Foundation/Foundation.h>

@interface MyData : NSObject

@property (nonatomic, retain) NSString *criterias;

@end

这是我设置映射器的方式:

- (RKResponseDescriptor*) getDataMapping
{
// Mapping
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[MyData class]];
[mapping addAttributeMappingsFromDictionary:@{
@"criteriasHeader": @"criteriasHeader"
}];

// Status code
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

// Descriptior
return [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodPOST pathPattern:nil keyPath:@"regions" statusCodes:statusCodes];
}

这里是我的请求函数:

- (void) runRequestWithType:(RequestType)type baseUrl:(NSString *)baseUrlString path:(NSString *)path parameters:(NSDictionary *)parameters mapping:(RKResponseDescriptor *) descriptor
{
// Print heeader and body from the request and the response
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("Restkit/Network", RKLogLevelDebug);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKLogConfigureByName("Restkit/ObjectMapping", RKLogLevelDebug);

// Set up the base url
NSURL *baseUrl = [NSURL URLWithString:baseUrlString];

//Run request in block
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseUrl];
[manager addResponseDescriptorsFromArray:@[descriptor]];

[manager.router.routeSet addRoute:[RKRoute routeWithClass:[MyData class] pathPattern:path method:RKRequestMethodPOST]];
//[manager getObjectsAtPath:path parameters:parameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[manager postObject:nil path:path parameters:parameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if ([self.delegate respondsToSelector:@selector(requestDidSucceedWithType:andResponse:)]) {
[self.delegate requestDidSucceedWithType:type andResponse:[mappingResult firstObject]];
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if ([self.delegate respondsToSelector:@selector(requestDidFailWithError:andError:)]) {
[self.delegate requestDidFailWithType:type andError:error];
}
}];
}

PS:我尝试使用较短的 JSON,效果很好。

我做错了什么吗?

你能帮帮我吗,谢谢。

最佳答案

这种错误来自RK下的JSON映射。默认是使用封装在 RKNSJSONSerialization 中的 NSJSONSerialization。您可以在那里放置一个断点,以了解更多有关错误的信息。到目前为止,我找到了 2 个来源:

  • NSJSONSerialization 不喜欢非 UTF8 数据。确保您从服务器接收它,或修改 RK 以正确地将数据转换为字符串(使用正确的编码)到 UTF8 数据(我目前拥有的最佳方式)。如果使用 CoreData,您可以查看第 586 行的 RKManagedObjectRequestOperation。
  • 在 iOS5 中存在错误。最简单的解决方法是使用另一个解析库

关于ios - 使用 RestKit 0.20.3 映射 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18192522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com