0

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.

enter image description here

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.

J.J.
  • 1,128
  • 2
  • 23
  • 62
  • Is your hosting server doing fine? – Saheb Roy Nov 24 '15 at 06:44
  • From log, I think the network connection is not stable, it breaks. May be you should check it on a stable network – Midhun MP Nov 24 '15 at 06:45
  • Yes server is working good in android application.This problem is only occur in ios application –  Nov 24 '15 at 06:45
  • How are you testing? is it with simulator or Device? How it is connected to internet ? wifi or 3g? – Bluewings Nov 24 '15 at 06:47
  • I am facing this problem in device not in the simulator –  Nov 24 '15 at 06:48
  • @Bhumica if the app works fine in simulator and not working as expected in device. I guess the problem is with your WiFI. try connecting to some other wifi network or via 3g once. – Bluewings Nov 24 '15 at 07:02
  • My net connection is also good. I have submitted the app to the appstore and they rejected my app because of this error. –  Nov 24 '15 at 07:04

1 Answers1

1

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];
Jatin Patel
  • 396
  • 3
  • 14