美文网首页
AFNetworking3.x Error Code:3840

AFNetworking3.x Error Code:3840

作者: 小北风sky | 来源:发表于2017-06-08 00:03 被阅读167次

    Error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

    解决办法:

    • 设置请求管理者,因为传递过去和接收回来的数据都不是json类型的,所以在这里要设置为AFHTTPRequestSerializer和AFHTTPResponseSerializer
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];// 请求
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];// 响应
    
    • 在success中将responseObject转换为一个字符串
    //转码
     NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    
    • 整体代码如下:
     ///AFNetworking 3.x
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];// 请求
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];// 响应
        
        [manager POST:urlString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
            
            //转码
            NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    
            
            NSLog(@"JSON: %@", string);
            
            
        } failure:^(NSURLSessionTask *operation, NSError *error) {
            
            NSLog(@"Error: %@", error);
            
            
        }];
    

    相关文章

      网友评论

          本文标题:AFNetworking3.x Error Code:3840

          本文链接:https://www.haomeiwen.com/subject/vlqzfxtx.html