0

I am using NSMutableURLRequest to get data from my server. The Codeigniter based api endpoint that I'm fetching from works 100% of the time from a browser for the website, but works roughly 75% of the time for the app.

I ran a simple test where I send post values to the server and spit them back to the app. I'm sending the same values every time and I only receive the correct response roughly 75% of the time.

Here is my app code.

// Request
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", URL_API, endpoint]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setHTTPMethod:method];
[request setValue:contentType forHTTPHeaderField:@"content-type"];
[request setTimeoutInterval:timeoutInterval];

DLog(@"bodyValues: %@", bodyValues);

// Set body
[request setHTTPBody:[self encodeDictionary:bodyValues]];

// Connection
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
    data = [[NSMutableData alloc] init];
}
else {
    [self showError];
}

Here is my server's code.

 header('HTTP/1.1 200 OK');
 header('Status: 200 OK');
 header('Access-Control-Allow-Origin: *');
 header('Access-Control-Request-Method: GET,POST');
 header('Access-Control-Allow-Headers: x-requested-with');
 header('Content-Type: application/x-json;charset=utf-8');
 header('Connection: keep-alive');
 print(json_encode($this->input->post()));

If there is any other code that I can provide just let me know. Thank you in advance!

Edit: Also, this is a problem on both the simulator and the device.

David Lackey
  • 33
  • 1
  • 8
  • Code looks just fine. You have to debug the server. Or if you don't have access to the server you could just call the urls of the server in the browser and check the response code. –  Jun 27 '12 at 14:52
  • Just to make sure I posted to the same url tons of times through both the browser and through terminal/cURL and didn't get a single null response – David Lackey Jun 27 '12 at 15:44

3 Answers3

0

The code looks fine, except in production you should probably put this on the background thread using NSOperationQueue.

What is timeoutInterval set to? Have you tried increasing it? My bet is your server isn't responding fast enough all the time.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • timeoutInterval was set to 20.0 seconds. I increased it to 200.0 seconds with no luck. I'm still getting a null response about 25% of the time. – David Lackey Jun 27 '12 at 14:19
0

I am wondering if the response is received before your NSMutableData ivar, data, has been initialized.

data = [[NSMutableData alloc] init];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
bbarnhart
  • 6,620
  • 1
  • 40
  • 60
0

In - (void)connectionDidFinishLoading:(NSURLConnection *)connection I commented out the following lines in my code below and for some reason I'm having solid dictionary responses every time.

NSError *error = nil;
//responseString = [NSString stringWithUTF8String:[data bytes]];
responseDictionary = NULL;
//if (responseString != (id)[NSNull null] && responseString.length != 0) {
    responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONWritingPrettyPrinted error:&error];
//}
David Lackey
  • 33
  • 1
  • 8