0

In my app I am establishing a connection with the server and try getting a JSON data from the server. When I sent the request, within moments a JSON object is generated in the server and it is sent back to the client (app). But the issue is 2 times out of 10 times, I am getting NULL value in the client even though the data is sent from the server ( not a NULL data but aJSONdata sinceJSON` data will be generated for all request).

I am not sure why I am getting NULL even though the data is generated in the server (By observing the server logs, I can say that)

The code is below

if(netStatus==ReachableViaWiFi || netStatus==ReachableViaWWAN){
    NSURL *url = [NSURL URLWithString:formattedURL];
    NSURLRequest* request = [NSURLRequest requestWithURL: url cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *jsonData, NSError *error)
     {
         if (error){

         }
         else{
             if ([NSString stringWithUTF8String:[jsonData bytes]]==NULL){
NSLog("The result is NULL");    
}

I am getting The result is NULL sometimes. I am not able to find a pattern. But this occurs 2 out of 10 times. I am not able to localise whether the issue is with the server or the app. Kindly let me know. Thanks for your time

Julian
  • 9,299
  • 5
  • 48
  • 65
Timothy Rajan
  • 1,947
  • 8
  • 38
  • 62
  • when ever you get null responce, check if that url contain space or not. –  Jan 11 '14 at 11:01
  • 1
    You should be using `initWithData:encoding:` to create the string. What is the JSON being sent back (same every time)? – Wain Jan 11 '14 at 11:04
  • It is same if request is sent immediately again and again. If the request is sent after a time interval of 5 mins the resultant Json is different – Timothy Rajan Jan 11 '14 at 11:09
  • @WasimMalek-I am getting the data generated in the server. So, URL is not an issue. – Timothy Rajan Jan 11 '14 at 11:14
  • Have implemented the following if condition if ([[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]==NULL){ }. Now I am getting JSON data displayed. Thanks @Wain – Timothy Rajan Jan 11 '14 at 12:22
  • If you share the url I can test it out :) – Joseph Chen Jan 11 '14 at 12:37

1 Answers1

1

If using initWithData:encoding: to create the string works then it is just an issue with your logging. Indeed, you could check the data length rather than trying to decode the text. Even better would be to use NSJSONSerialization with the data directly, supplying appropriate options and logging the resulting object and error.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I am facing a minor issue in the code ([[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]==NULL){ }. Even though a NULL value is returned, (in the log it is displayed as "[]") the block when condition is NULL is not getting executed. What is the value of [] in iOS. Is it NULL or we need to equate it to other than NULL. Please let me know. – Timothy Rajan Jan 11 '14 at 14:56
  • It would look like an empty array. You should decode the JSON and use the `count` of the returned object (array or dictionary). – Wain Jan 11 '14 at 16:19