Reputation:
I am working on an application in which I am using web-services method to fetch data from server.
Below is my code.
responseData = [NSMutableData data];
NSString *service = @"/getActivity.php";
NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
NSString *urlLoc = [fileContents objectForKey:@"URL"];
urlLoc= [urlLoc stringByAppendingString:service];
loginUser = [[NSUserDefaults standardUserDefaults]valueForKey:@"username"];
NSLog(@"%@",loginUser);
NSLog(@"%@",selectUsername);
// this is correct
//NSString *requestStringg = [NSString stringWithFormat:@"{\"?username\"=\"%@\"&\"follower_username\"=\"%@\"}",loginUser,selectUsername];
NSDate * date = [NSDate date];
// NSLog(@"Date:%@",date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *date_str = [dateFormat stringFromDate:date];
NSString *requestString = [NSString stringWithFormat:@"username=%@&follower_username=%@&follower_created_date=%@",loginUser,selectUsername,date_str];
NSData *postData = [requestString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlLoc]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
postFollowUnfollowConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"error :%@",error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection == postNotificationConnection)
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseString);
}
}
My web-services are working fine, but after the 4th or 5th time of calling web-services it enters into the didFailWithError
, showing me the below error.
I have tried every possible solution for this error, but nothing helps me. This error is being produced in iOS 8 and iOS 9.
Any help would be appreciated.
Upvotes: 0
Views: 1570
Reputation: 406
1) It is iOS 8 problem but it can be solve by using
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
instead of
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
2) other on solution is that increase RequestTime:
[request setTimeOut:180];
Upvotes: 1