ios - 如何解决 NSURLError : The network connection was lost?

标签 ios objective-c iphone xcode nsurlconnection

我正在开发一个应用程序,我在其中使用网络服务方法从服务器获取数据。

下面是我的代码。

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);

     }

}

我的网络服务工作正常,但在第 4 次或第 5 次调用网络服务后,它进入了 didFailWithError,向我显示以下错误。

enter image description here

我已经为这个错误尝试了所有可能的解决方案,但没有任何帮助。此错误在 iOS 8 和 iOS 9 中产生。

如有任何帮助,我们将不胜感激。

最佳答案

1) 这是 iOS 8 的问题,但可以使用

[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

代替

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

2) 其他解决方案是增加 RequestTime:

[request setTimeOut:180];

关于ios - 如何解决 NSURLError : The network connection was lost?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887043/

相关文章:

ios - 如何使用 FlowLayout 确定 UICollectionView 的高度

iphone - GPS不会停止更新

ios - 设备图库图像未显示在 ios7 的设备中

iphone - UIWebView 在 iOS9 上不显示图像,SafariViewController 根本不加载 HTML 文件?

ios - 我怎样才能给 PHAsset 一个标志?

xcode - 如何使用 SQlite Random()

iphone - 使用 CLGeocoder 重复地理编码后出现 kCLErrorDomain 错误 2

iOS 8 UITableViewRowAction : Swipe Left, 图片?

iphone - 如何拆分 UIView

ios - Sprite Kit 物理碰撞问题