2012-11-08 60 views
5

我正在編寫一個調用JSON Web服務的應用程序。當iPhone連接到WiFi網絡時,它工作正常。但是,當它使用蜂窩數據網絡時,它不起作用。我收到[NSJSONSerialization JSONObjectWithData:data options:kNilOptions錯誤:&錯誤]返回的錯誤。這裏是我的代碼:返回iOS調用Web服務在4g上不起作用

-(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; 
} 

和錯誤是:像你的數據你是JSON編碼或結果混淆

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

返回的響應是什麼? – simbolo

+0

你有任何解決方案嗎? – brush51

回答

0

聲音聽起來我。

這裏最好的辦法是打印出所有的原始數據或者使用Charles Proxy(http://charlesproxy.com)這樣的東西來攔截網絡請求並查看正在發生的事情。

相關問題