lubilis
lubilis

Reputation: 4170

Incomplete json response from php to iOS app

In my iOS application i have an asynchronous connection that calls a php script on my server. This script makes a query to the database and give a response with json_encode.

This is the script:

$result = mysql_unbuffered_query($query);
if ($result){
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $output[] = $row;
    }
    if($output!=null)       
        echo json_encode($output);
    else 
        echo "0";
}

All works good when the response is short, but when i have a long response (like 500 characters), i receive an incomplete json as this example:

[{"user":"1","password":"password","tel":"3333333333","description":"some text, some text, some text","data":"2013-10-13 09:53:54"}, {"user":"1","password":"password","tel":"3333333333","description":"some text, so

In my iOS application, i use this code to decode the json:

json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

If i receive a full json all works good, else if i receive an incomplete json i send a new request. When json content is very very long, the json received is incomplete, but sometimes, after some requests, the response is good anyway.

/* UPDATE QUESTION */

Perform request:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:advertURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];

NSString *queryString = [NSString stringWithFormat: @"%@", query];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
myConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Get response:

//in NSURL method didReceiveResponse

ResponseData = [[NSMutableData alloc] init];

//in NSURL method didReceiveData

[ResponseData appendData:data];

NSString *result = [[NSString alloc] initWithData:ResponseData encoding:NSUTF8StringEncoding];
json = [NSJSONSerialization JSONObjectWithData:Responsedata options:kNilOptions error:&error];

if(!json){
    //incomplete json!
}
else{
    //good json!
}

Why this happen? How can i solve this?

Hope i explained myself, thank you.

Upvotes: 2

Views: 731

Answers (1)

Rob
Rob

Reputation: 438487

This problem is common when people use the NSURLConnectionDataDelegate methods (or the NSURLSessionDataDelegate methods), and don't realize that it may require multiple calls to didReceiveData to receive the entire payload.

So, what you can do is:

  • instantiate a NSMutableData in didReceiveResponse;

  • have didReceiveData only append data to the NSMutableData, recognizing that for larger payloads, it may take multiple calls to this method before all of the data is received; and

  • in connectionDidFinishLoading: (if using NSURLConnection; if using NSURLSession, use the URLSession:task:didCompleteWithError: method), then proceed with the parsing now that all data has been received.

Upvotes: 2

Related Questions