ios (Cocoa 错误 3840。)”(字符 0 周围的值无效。)AFNetworking

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25986129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:43:05  来源:igfitidea点击:

(Cocoa error 3840.)" (Invalid value around character 0.) AFNetworking

iosobjective-cjsoncocoa-touchafnetworking-2

提问by Jonathan

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

使用 GET 方法从服务器检索文件时出现以下错误:

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.}

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.

我尝试了许多不同的方法,我相信这可能与我试图获取的文件中的 JSON 格式有关。

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);
     }];

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

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

回答by Brad Allred

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 AFHTTPResponseSerializercan be used for responses that are not JSON.

从评论中的讨论来看,您的 GET 请求似乎成功(响应代码 200),但响应正文不是有效的 JSON(也不是 JSON 片段),正如您使用AFJSONResponseSerializer. 基本AFHTTPResponseSerializer可用于非 JSON 的响应。

回答by Santi Pérez

I am pretty sure that you have a valid response from server but your response is not a valid format in JSON, probably because you have carachters in front of first { . Please try this: Put the same URL address manually in your browser and you will see the culprit in the response. Hope it helped.

我很确定您有来自服务器的有效响应,但您的响应不是 JSON 中的有效格式,可能是因为您在 first { 前面有 carachters。请尝试以下操作:在浏览器中手动输入相同的 URL 地址,您将在响应中看到罪魁祸首。希望它有所帮助。

回答by berkat0789

Hey guys this is what I found to be my issue: I was calling Alamofire via a function to Authenticate Users: I used the function "Login User" With the parameters that would be called from the "body"(email: String, password: String) That would be passed

嘿伙计们,这就是我发现的问题:我通过一个函数调用 Alamofire 来验证用户:我使用了函数“登录用户”以及将从“正文”调用的参数(电子邮件:字符串,密码: String) 那将被传递

my errr was exactly:

我的错误正是:

optional(alamofire.aferror.responseserializationfailed(alamofire.aferror.responseserializationfailurereason.jsonserializationfailed(error domain=nscocoaerrordomain code=3840 "invalid value around character 0." userinfo={nsdebugdescription=invalid value around character 0

可选(alamofire.aferror.responseserializationfailed(alamofire.aferror.responseserializationfailurereason.jsonserializationfailed(error domain=nscocoaerrordomain code=3840“字符0周围的值无效。” userinfo={nsdebugdescription=字符0周围的值无效)

character 0 is the key here: meaning the the call for the "email" was not matching the parameters: See the code below

字符 0 是这里的关键:意味着对“电子邮件”的调用与参数不匹配:请参阅下面的代码

func loginUser(email: String, password: String, completed: @escaping downloadComplete) { let lowerCasedEmail = email.lowercased()

func loginUser(email: String, password: String, completed: @escaping downloadComplete) { let lowerCasedEmail = email.lowercased()

    let header = [
        "Content-Type" : "application/json; charset=utf-8"
    ]
    let body: [String: Any] = [
        "email": lowerCasedEmail,
        "password": password
    ]

    Alamofire.request(LOGIN_USER, method: .post, parameters: body, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
        if response.result.error == nil {

            if let data = response.result.value as? Dictionary<String, AnyObject> {
                if let email = data["user"] as? String {
                    self.userEmail = email
                    print(self.userEmail)
                }
                if let token = data["token"] as? String {
                    self.token_Key = token
                    print(self.token_Key)
                }

"email" in function parameters must match the let "email" when parsing then it will work..I no longer got the error...And character 0 was the "email" in the "body" parameter for the Alamofire request:

函数参数中的“email”在解析时必须与 let“email”匹配,然后它才能工作......我不再收到错误......并且字符 0 是 Alamofire 请求的“body”参数中的“email”:

Hope this helps

希望这可以帮助