iOS Calling Web Service不适用于4g

时间:2012-11-08 02:23:34

标签: ios json web-services 4g

我正在编写一个调用JSON Web服务的应用程序。当iPhone连接到WiFi网络时,它可以正常工作。但是,当它使用蜂窝数据网络时,它不起作用。我收到[NSJSONSerialization JSONObjectWithData:数据选项:kNilOptions错误:&错误]返回的错误。这是我的代码:

-(BOOL) CallService {
    NSError *error;
    NSData *paramData = [NSJSONSerialization dataWithJSONObject:self.parameter    options:kNilOptions error:&error];

    NSString *serviceUrl = [[NSString alloc] initWithFormat:@"%@%@", self.webHost, self.serviceName];
    NSURL *url = [NSURL URLWithString:serviceUrl];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [paramData length]]  forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:paramData];
    NSError *errorReturned = nil;
    NSURLResponse *theResponse =[[NSURLResponse alloc]init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&theResponse
                                                     error:&errorReturned];

    BOOL retVal = FALSE;

    if (errorReturned){
        //...handle the error
        NSLog(@"%@", errorReturned.description);
    }
    else {
        self.serviceResult = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        NSLog(@"%@", error);
        retVal = (BOOL)[self.serviceResult objectForKey:@"Success"];
    }

    return retVal;
}

返回的错误是:

purgeIdleCellConnections: found one to purge conn = 0x1d04d710
2012-11-07 20:17:43.776 iPressBoxx-iPhone[733:907] 
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=0x1d04eff0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

1 个答案:

答案 0 :(得分:0)

听起来听起来像你是JSON编码的数据或结果混淆了。

最好的办法是打印出所有原始数据或使用Charles Proxy(http://charlesproxy.com)之类的内容拦截网络请求,看看发生了什么。