0

This may be a duplicate of iOS JSON Error: NSDebugDescription=Garbage at end, but that didn't get answered.

Here's my code:

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:MY_URL]];

    [request:@"POST"];
    [request setAllowsCellularAccess:YES]; // not really needed
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]];

    // Create url connection and fire request
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Then later:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    _jsonData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_jsonData appendData:data];
}


- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
              willCacheResponse:(NSCachedURLResponse*)cachedResponse {
    return nil;
}

When the JSON response is large (>110k or so), it can be parsed perfectly when there's a wifi connection, but gives an error when on a carrier network: iOS JSON Error: NSDebugDescription=Garbage at end. In both cases, didReceiveData is called twice.

Any ideas? Thanks!

Community
  • 1
  • 1
ScottyB
  • 2,167
  • 1
  • 30
  • 46
  • In your didReceiveResponse method check the NSURLResponse object expectedContentLength variable. see if the expected length and NSdata length ends up being the same. Also use [network link conditioner on osx](http://nshipster.com/network-link-conditioner/) to test varies types of network – Shams Ahmed Sep 13 '14 at 23:07
  • Thanks! The expectedContentLength is -1 (NSURLResponseUnknownLength) because the "server HTTP response does not include a Content-Length header." I have control over that, but the expectedContentLength is also -1 when the iPhone is using wifi as well. The mimeType for both is text/html. The results of initWithURL:MIMEType:expectedContentLength:textEncodingName: is the same for both as well. – ScottyB Sep 14 '14 at 04:12
  • So I tried adding a header just containing content-length to the http response, and that seemed to fix the problem. Is there a default maximum expected by iOS if not specified in the header? – ScottyB Sep 14 '14 at 05:01
  • its always varies on the http body, there never a default length as you never be sending the same data all the time – Shams Ahmed Sep 14 '14 at 11:46
  • What I'm saying is that iOS seems to assume a maximum length if the response does not include a content-length header, and that the maximum length is different depending on whether the connection is via wifi (longer) or 3G/4G (shorter). At least that would explain why the response without the content-length header worked with wifi and not 3G/4G, and why adding the content-length header resolved the problem. – ScottyB Sep 14 '14 at 18:11

0 Answers0