使用 JSON 的 Cocoa 错误 3840 (iOS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14171111/
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-30 21:44:18  来源:igfitidea点击:

Cocoa error 3840 using JSON (iOS)

objective-ciosjsoncocoa-touch

提问by iosdevrocks

I'm trying to send data to a server and receive the response in JSON format. The problem is that the server has to return "success" or "fail" but it returns "(null)".

我正在尝试将数据发送到服务器并以 JSON 格式接收响应。问题是服务器必须返回“成功”或“失败”,但它返回“(空)”。

Here's the returned error:

这是返回的错误:

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=XXXXXXXXX {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

错误域=NSCocoaErrorDomain 代码=3840“无法完成操作。(Cocoa 错误 3840。)”(JSON 文本未以数组或对象开头,并且未设置允许片段的选项。) UserInfo=XXXXXXXXX {NSDebugDescription=JSON 文本没有以数组或对象开头,并且没有设置允许片段的选项。}

Is it possible that the error is in the server script?

错误是否可能在服务器脚本中?

Here's my function to send the data and receive the response:

这是我发送数据和接收响应的函数:

- (void) putData:(NSString *)parameter valor:(NSString *)valor {

    NSString *rawString = [NSString stringWithFormat:@"%@=%@", parameter, valor];
    NSData *data = [rawString dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://www.xxx.xxx/xxx.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"PUT"];
    [request setHTTPBody:data];
    NSURLResponse *response;
    NSError *error;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSLog(@"responseData: %@ error: %@", json, error);
}

采纳答案by Stu

Unless you pass the option NSJSONReadingAllowFragmentsto [NSJSONSerialization JSONObjectWithData:options:error:]the response from the server must be valid JSON with a top level container which is an array or dictionary.

除非您将选项传递NSJSONReadingAllowFragments[NSJSONSerialization JSONObjectWithData:options:error:]来自服务器的响应,否则必须是具有顶级容器(数组或字典)的有效 JSON。

for example:

例如:

    { "response" : "Success" }

P.S. If you want a mutable dictionary you must also include NSJSONReadingMutableContainersin your options.

PS 如果你想要一个可变的字典,你还必须包括NSJSONReadingMutableContainers在你的选项中。

回答by Vandit Mehta

It may possible that, the response from your server doesn't contain valid JSON.

您的服务器的响应可能不包含有效的 JSON。

Technically, The JSON object must be start with either an "array" or an "object (dictionary)".

从技术上讲,JSON 对象必须以“数组”或“对象(字典)”开头。

So, Whatever your server is returning isn't.

因此,无论您的服务器返回的是什么。

And, you can force the JSON to be consumed regardless by using the NSJSONReadingAllowFragments option.

而且,您可以强制使用 JSON,而不管使用 NSJSONReadingAllowFragments 选项。

by using ,

通过使用 ,

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

回答by codeburn

You can get this issue if you're connected to VPN on your iOS device.

如果您在 iOS 设备上连接到 VPN,则可能会遇到此问题。