2012-11-08 9 views
5

Sto scrivendo un'app che chiama un servizio Web JSON. Funziona perfettamente quando l'iPhone è connesso a una rete WiFi. Ma quando utilizza la rete dati cellulare non funziona. Ricevo un errore restituito da [NSJSONSerialization JSONObjectWithData: opzioni dati: errore kNilOptions: errore &]. Ecco il mio codice:iOS Chiamare il servizio Web non funziona su 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; 
} 

e l'errore viene restituito è:

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

Qual è la risposta che viene restituita? – simbolo

+0

hai qualche soluzione per questo? – brush51

risposta

0

Suoni a me come se fossi dati sei JSON codifica o risultato viene mescolati.

La soluzione migliore qui è stampare tutti i dati grezzi o utilizzare qualcosa come Charles Proxy (http://charlesproxy.com) per intercettare le richieste di rete e vedere cosa sta succedendo.