AFNetworking 请求报错 NSCocoaErrorDomain Code=3840 查看Response数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nadeal/article/details/89307951
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=0x9152780 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

有时候请求后台,爆出3840的错误码,这是返回结果格式解析错误。这时候又不能在项目代码中直接查看到返回的数据。那就只能在AFNetWorking的源码中去查看返回的Response。在AFURLSessionManager.m文件中,找到代码,并插入打印日志代码,将返回的结果打印出来,

#pragma mark - NSURLSessionTaskDelegate

- (void)URLSession:(__unused NSURLSession *)session
              task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
    __strong AFURLSessionManager *manager = self.manager;

    __block id responseObject = nil;

    __block NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
    userInfo[AFNetworkingTaskDidCompleteResponseSerializerKey] = manager.responseSerializer;

    //Performance Improvement from #2672
    NSData *data = nil;
    if (self.mutableData) {
        data = [self.mutableData copy];
        //We no longer need the reference, so nil it out to gain back some memory.
        self.mutableData = nil;
    }

    if (self.downloadFileURL) {
        userInfo[AFNetworkingTaskDidCompleteAssetPathKey] = self.downloadFileURL;
    } else if (data) {
        userInfo[AFNetworkingTaskDidCompleteResponseDataKey] = data;
    }
    // 添加下面这两行代码查看服务器的返回是什么,(猜测是服务器的问题,所以需要服务器端,添加 try...catch 捕获异常,并返回给调用方)
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", dataStr);

这样可以快速的帮助我们找到问题是什么。

猜你喜欢

转载自blog.csdn.net/nadeal/article/details/89307951