(Cocoa error 3840。)“ (字符0周围的值无效。)AFNetworking [英] (Cocoa error 3840.)" (Invalid value around character 0.) AFNetworking

查看:395
本文介绍了(Cocoa error 3840。)“ (字符0周围的值无效。)AFNetworking的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GET方法从服务器检索文件时,我遇到了以下错误:

I've been getting the following error when using the GET method to retrieve a file from a server:

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x16e81ed0 {NSDebugDescription=Invalid value around character 0.}

我尝试过很多不同的事情,我相信它可能是与JSON格式的文件,

I've tried a number of different things and I believe it could be something to do with the JSON format on the file that I'm trying to get.

以下是我一直在使用的代码:

Here is the code I've been using:

_username = @"JonDoe";
NSDictionary *parameters = @{ @"username" : _username};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

[manager GET:@"http://.........."
  parameters:parameters
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"JSON: %@", responseObject);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);
     }];

我的POST方法工作正常。我只是似乎无法解决这个问题与GET。有任何想法吗?谢谢。

My POST method works fine. I just can't seem to fix this issue with the GET. Any ideas? Thank you.

推荐答案

根据评论中的讨论判断,您的GET请求已成功(响应代码200)响应正文不是您使用 AFJSONResponseSerializer 所请求的有效JSON(也不是JSON片段)。基本 AFHTTPResponseSerializer 可用于非JSON的响应。

Judging by the discussion in the comments it appears that your GET request is successful (response code 200), but the response body is not valid JSON (nor a JSON fragment) as you have requested by your use of AFJSONResponseSerializer. A basic AFHTTPResponseSerializer can be used for responses that are not JSON.

这篇关于(Cocoa error 3840。)“ (字符0周围的值无效。)AFNetworking的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆