I a using STHTTPRequest to fetch JSON data in an IOS App. Please see the code below. When i am testing on Iphone 6 IOS8.0, Sometimes the code is going into error block. Its not happening always , only sometimes. Could anyone help ? Thank u
if(showActivity)
{
[self ShowActivityIndicatorWithTitle:@"Loading..."];
}
STHTTPRequest *request1 = [STHTTPRequest requestWithURLString:[NSString stringWithFormat:@"%@%@",kBaseUrl,api]];
[request1 setTimeoutSeconds:120.0f];
[request1 setHeaderWithName:@"Content-Type" value:@"application/x-www-form-urlencoded"];
[request1 setHTTPMethod:@"POST"];
request1.rawPOSTData = [postData dataUsingEncoding:NSUTF8StringEncoding];
request1.completionDataBlock = ^(NSDictionary *headers, NSData* data)
{
[self HideActivityIndicator];
if(handler != nil)
{
NSError* connectionError;
handler(JSONObjectFromData(data),connectionError);
}
};
request1.errorBlock=^(NSError *error)
{
NSLog(@"Error: %@", [error localizedDescription]);
if(error != nil)
{
[[[UIAlertView alloc] initWithTitle:@"Connection Error !" message:kAlertInternetConnection delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
[self HideActivityIndicator];
}
};
[request1 startAsynchronous];
I think i found the solution. Basically IOS takes the "Keep Alive" Parameter from first response, and thinks the connection is persistent. When the next JSON call is made, IOS is trying to use existing connection, which is expired. I have added header('Connection: close'); in each PHP web service. I am telling ios in each web service that the connection is closed. I am not sure if it is good way to do it. I have tested for 20 min on device. Its working. I appreciate any thoughts on this.