AFNetworking 3.0 未从响应中获取 json

Posted

技术标签:

【中文标题】AFNetworking 3.0 未从响应中获取 json【英文标题】:AFNetworking 3.0 not getting json from response 【发布时间】:2016-06-26 04:34:22 【问题描述】:

我正在发布请求以获取响应 responseObject 但数据格式。为什么它不是以 json 格式出现。

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSString *urlstr=[SERVER_API_URL stringByAppendingString:methodType];
    [manager POST:urlstr parameters:input progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) 
        NSLog(@"success! data=%@",responseObject);
        NSLog(@"responseObject==%@",responseObject);

     failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) 
        NSLog(@"error: %@", error);
    ]

;

这里是我在 NSLog 中得到的 responseObject

success! data=<227b2261 63636573 735f746f 6b656e22 3a224d58 31563255 736c3053 47367654 706f2d4d 76346459 672d3369 35684c4e 796d4245 4e674e4d 56574b5a 6c394476 3937536e 364a4e7a 3952524b 556d4f50 717a485f 7748416e 63675849 70626e43 35703647 4c4f324d 7a773761 6b366d4d 6c4b6465 36766d62 3769434a 4973336c 3544674a 74345a4f 48523338 786c5270 554a4e56 5762356c 39347067 72417978 34555a47 35376444 456a4c62 39394d4b 55517358 546a3933 3751614e 526f5073 4c437859 4b5a7a6e 79483533 4f553555 4266457a 6f396732 5f767262 31736278 58334174 63467072 4a674d70 45682d45 33613750 346d3344 2d64706a 5a575365 7a4a6155 74463256 5a463733 6c716c32 5933222c 22746f6b 656e5f74 79706522 3a226265 61726572 222c2265 78706972 65735f69 6e223a38 36333939 7d22>

当我从邮递员客户端传递请求时,我得到了所有正确的数据。这是屏幕截图。

【问题讨论】:

而不是manager.responseSerializer = [AFHTTPResponseSerializer serializer];为什么你不使用manager.responseSerializer = [AFJSONResponseSerializer serializer]; 我使用了 manager.responseSerializer = [AFJSONResponseSerializer 序列化器];然后我得到这个错误域 = NSCocoaErrorDomain 代码 = 3840 “JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。” UserInfo=NSDebugDescription=JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。 @VivekMolkar 你可以用[AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];代替[AFJSONResponseSerializer serializer] manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];我用这个然后我得到这个错误错误:错误域=NSCocoaErrorDomain代码=3840“垃圾结束。” UserInfo=NSDebugDescription=垃圾结束。 您能否再次从其他客户端确认您的响应除了 JSON 之外没有其他元素? AFAIK 当响应中返回一些错误或非 json 元素时会发生此错误。 【参考方案1】:

按以下方式更改您的代码:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

NSString *urlstr=[SERVER_API_URL stringByAppendingString:methodType];
[manager POST:urlstr parameters:input progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) 
    NSLog(@"[success]: %@",responseObject);

 failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) 
    NSLog(@"[error]: %@", error);
]

【讨论】:

对于 3.0 版,AFJSONResponseSerializer 仍然不接受 MIME 类型为 application/json 的响应 是的,用manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]; 替换manager.responseSerializer = [AFJSONResponseSerializer serializer]; 就成功了。【参考方案2】:

设置可接受的内容类型:

[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithArray:@[@"application/json"]]];

然后把回复变成NSDictionary

NSDictionary *response = responseObject;
NSLog("api response: %@", response);

记得处理任何错误。


附注:如果您使用的是自签名 SSL,您可以通过以下方式允许无效证书:

manager.securityPolicy.allowInvalidCertificates = YES;
manager.securityPolicy.validatesDomainName = NO;

【讨论】:

我在失败中遇到此错误 Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo=NSDebugDescription=垃圾结束。 那么你的 JSON 就搞砸了。首先用纯文本验证响应。【参考方案3】:

我得到了同样的错误,但是你修复了它,指出 JSON 将在哪里打印

之前

NSLog(@"JSON usando AFNetworking: %@",responseObject);

现在可以使用了

NSLog(@"JSON usando AFNetworking: %@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

【讨论】:

以上是关于AFNetworking 3.0 未从响应中获取 json的主要内容,如果未能解决你的问题,请参考以下文章

在故障块 AFNetworking 3.0 上获取 responseObject

UIImageView+AFNetworking 未从服务器检索最新图像

来自 GET 请求的 AFNetworking 3.0 404 响应

我如何在 AFNetworking 3.0 中获取 allHTTPHeaderFields

未从 ADFS 3.0 获取用于 OAuth2 访问令牌的 JWT 中的用户身份

AFNetworking 3.0 GET 请求在 iOS 中保存到类中