5

I am writing an app that calls a JSON web service. It works fine when the iPhone is connected to a WiFi network. But when it is using the cellular data network it does not work. I am getting an error returned from the [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]. Here is my code:

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

And the error being returned is:

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.}
manlio
  • 18,345
  • 14
  • 76
  • 126
Dan
  • 195
  • 1
  • 3
  • 10

1 Answers1

0

Sounds sounds to me like you're data you're JSON encoding or result is mixed up.

Best bet here is to print out all the raw data or use something like Charles Proxy (http://charlesproxy.com) to intercept the network requests and see what's going on.

Glen T
  • 1,550
  • 1
  • 13
  • 22