【问题标题】:error parsing JSON response Cocoa error 3840解析 JSON 响应时出错 Cocoa 错误 3840
【发布时间】:2014-03-20 06:58:00
【问题描述】:

当我想将 json 发布到 Web 服务器时,它会崩溃并显示:

Warning, status code of response was not 200, it was 400
2014-03-20 01:15:54.407 iBeacons[6669:60b] error parsing JSON response: 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=0x156b6440 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

2014-03-20 01:15:54.408 iBeacons[6669:60b] 返回字符串:

400 必需的字符串参数 'beaconsMapping' 不存在 必需的字符串参数 'beaconsMapping' 不存在

    if ([NSJSONSerialization isValidJSONObject:sendData]) 
    {
    NSError *error;
    NSLog(@"1");
    NSData *jsonData=[NSJSONSerialization 
    dataWithJSONObject:sendData options:
    NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];
    NSLog(@"2");
    NSString *json=[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",json);
    NSString *reqData=[@"beaconsMapping=" stringByAppendingString:json];
    NSLog(@"%@",reqData);
    NSData *postDatas=[NSData dataWithBytes:[reqData UTF8String] length:[reqData length]];
    NSString *postLength=[NSString stringWithFormat:@"%d",[postDatas length]];
    NSMutableURLRequest *requestPost=[NSMutableURLRequest requestWithURL:dataUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    NSLog(@"%@",dataUrl);
    [requestPost setHTTPMethod:@"POST"];
    [requestPost setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [requestPost setValue:@"application/json"forHTTPHeaderField:@"Accept"];
    [requestPost setValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
    [requestPost setHTTPBody:postDatas];

    NSError *requestError=nil;
    NSURLResponse *response = nil;
    NSData *data=[NSURLConnection sendSynchronousRequest:requestPost returningResponse:&response error:&requestError];
    if (requestError == nil) {
        if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
            NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
            if (statusCode != 200) {
                NSLog(@"Warning, status code of response was not 200, it was %d", statusCode);
            }
        }

        NSError *error;
        NSDictionary *returnDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
        if (returnDictionary) {
            NSLog(@"returnDictionary=%@", returnDictionary);
        } else {
            NSLog(@"error parsing JSON response: %@", error);

            NSString *returnString = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
            NSLog(@"returnString: %@", returnString);
        }
    } else {
        NSLog(@"NSURLConnection sendSynchronousRequest error: %@", requestError);
    }


}

//我的json输出:

beaconMapping=[
    {
        "floorplanId": "mcc",
        "id": "mcc.b",
        "beaconIds": [
            {
                "distance": "3.873",
                "beaconId": "identityId:Major: 44643, Minor: 29271 x:23.6 y:-21.3 z:-50.2"
            }
        ],
        "locationId": "b"
    }
]//thewebserverrequirestheformatlikethat
 [
    {
        "floorplanId": "mcc",
        "locationId": "c",
        "id": "mcc.c",
        "beaconIds": [

        ]
    },
    {
        "floorplanId": "mcc",
        "locationId": "b",
        "id": "mcc.b",
        "beaconIds": [
            {
                "beaconId": "identityId:Major: 44643, Minor: 29271 x:58.0 y:124.6 z:-360.3",
                "distance": "3.202"
            }
        ]
    },
    {
        "floorplanId": "mcc",
        "locationId": "a",
        "id": "mcc.a",
        "beaconIds": [

        ]
    }
]

// 我的网络服务器邮政编码:

    @RequestMapping(value = "/beacons/{floorplanId}", method = RequestMethod.POST)
public @ResponseBody
Set<BeaconsAtFloorplanLocation> setBeacons(
        @PathVariable("floorplanId") String floorplanId,
        @RequestParam("beaconsMapping") String beaconMappingJson)
        throws Exception {

    Set<BeaconsAtFloorplanLocation> beacons = (new MCCObjectMapper())
            .readValue(beaconMappingJson,
                    new TypeReference<Set<BeaconsAtFloorplanLocation>>() {
                    });

    beaconsLoader_.setBeaconMapping(floorplanId, beacons);

    return beacons;
}

【问题讨论】:

  • 发布您的相关代码
  • 可能是json编码发现了问题。
  • 我已添加代码,请查看,谢谢
  • beaconMapping=等号在json格式中无效
  • 但是我的 http post 需要一个名为 beaconsMapping 的参数来接收发送的数据,我应该如何解决它。我已经在上面发送了 java web 服务器代码

标签: ios objective-c json post


【解决方案1】:

尝试删除

Content-Length

Accept

Content-Type

来自您的代码。它可能会帮助你。 尽管我的 json 对象是有效的 json,但我对 json WCF 也有同样的问题。 我从代码中删除了所有这 3 个并且它起作用了。服务器不接受这些。 不知道为什么。。

干杯

【讨论】:

    【解决方案2】:

    我已经解决了这个问题,删除

    [requestPost setValue:@"application/json"forHTTPHeaderField:@"Accept"];
    [requestPost setValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
    

    它会起作用的。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        您在编码 json 时可能没有提供正确的格式数据。

        你用的是json序列化吗?

        在执行之前尝试使用 NSLog 来查看您的数据是否正确,您创建一个 NSError *error 并将其放入您的 json 编码中以检查发生了什么,然后再将其发布到您的网络服务器。

        【讨论】:

        • 是的,当输出数据看起来正确并且我发布了我的代码和 json 格式时,你能帮我再检查一次吗?谢谢!
        猜你喜欢
        • 1970-01-01
        • 2012-06-25
        • 1970-01-01
        • 1970-01-01
        • 2013-02-21
        • 2012-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多