NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)"

原文:

http://stackoverflow.com/questions/19874935/afnetworking-2-0-post-issue-cocoa-error-3840json-text-did-not-start-with-array

问:

I am attempting to call api.php on my local server(using MAMP). The server side api.php is being called but the content of _POST inside the php code contains the following 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=0x15d7bdd0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

My app that is trying to send the JSON POST request to the api.php is an iOS app using AFNetworking 2

This is my request code:

- (void)postUpdateRequest
{
    if (!dataModel)
        dataModel = [[DataModel alloc] init];

    NSDictionary *params = @{@"foo": @"bar2"};

    NSLog(@"%@",params);

/*    NSDictionary *params = @{@"cmd":@"update",
                             @"user_id":[dataModel userId],
                             @"token":[dataModel deviceToken]
                             };//@"ip_address":[dataModel getIPAddress]};*/

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager POST:ServerApiURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

I looked all over the internet but can't find an answer for my specific problem. No matter what I do I get the same error back.

Why is my JSON not properly formatted? I also can't seem to find a way to sniff what JSON I am actually sending.

Thank you.

 

答:

When you use the API as in your example, your HTTP message will be composed by using a Content-Type application/x-www-form-urlencoded. Internally, your param dictionary are encoded by AFN (although not strictly correct as specified by w3c), and set as the request body.

Since, you did not specify an Accept header, the server is free to choose the content type for a possible response data (if any).

When you receive a response, you need to always check the HTTP status code and the content type of a response body (if any).

It's likely, the server returned a status code indicating some issue and a response body containing an "error response" in some different kind of content type than you expect (for example, it returned text/html).

 

有关中文解决方案:

使用AFNetworking 框架 解析数据 报错提示

数据请求失败

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

解决:

AFNetWorking的JSON解析默认库是使用的AFJSONRequestOperation模式,只支持text/json,application/json,text/javascript,所以如果出现code=-1016错误则说明当前的JSON解析模式是text/html,所以要加上这段代码:

[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];//加上这句话

 

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",kDMBaseURL]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];//加上这句话

    AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        DMLog(@"str %@",JSON); 

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

        DMLog(@"数据请求失败%@",error);

    }];

    [jsonOperation start];

让他支持text/html模式就可以解决了

 

数据请求失败

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

解决:

后台返回的不是一个有效的JSON string 所以库里面转化的时候一个error就抛出了

就是最后一句说的,肯定是服务器端,在你指定的输出 JSON 数据之前截断输出其他数据,所以出现错误。可以去服务器段判断。

 

转载于:https://www.cnblogs.com/yjg2014/p/3822339.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Homebrew是一款用于在Mac OS上安装软件包的开源软件管理器。当在使用Homebrew时出现"the operation couldn't be completed. unable to locate a java runtime"的错误时,这意味着Homebrew无法定位到Java运行时环境。 出现这个错误的原因可能是由于没有正确安装Java运行时环境或者环境变量配置错误。解决这个问题的方法如下: 1. 确保已正确安装Java运行时环境。可以从Oracle官网下载并安装最新版本的Java JDK/JRE。 2. 验证Java的安装。在终端中运行"java -version"命令,确保Java成功安装并且能够正确运行。 3. 检查环境变量配置。在终端中运行"echo $JAVA_HOME"命令,确保JAVA_HOME环境变量已正确配置并指向Java的安装路径。 - 如果未配置JAVA_HOME环境变量,可以通过编辑"~/.bash_profile"或者"~/.zshrc"文件来添加配置,例如: ``` export JAVA_HOME=/Library/Java/JavaVirtualMachines/<VERSION>/Contents/Home export PATH=$PATH:$JAVA_HOME/bin ``` 4. 更新Homebrew。在终端中运行"brew update"命令,确保Homebrew是最新版本。 5. 尝试重新安装Homebrew。在终端中运行以下命令: ``` /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` - 如果仍然无法解决问题,可以尝试卸载并重新安装Homebrew。 这些步骤应该能够解决"the operation couldn't be completed. unable to locate a java runtime"错误,并使Homebrew能够正确定位Java运行时环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值