Afnetworking: Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.”

Created on 3 Feb 2015  ·  8Comments  ·  Source: AFNetworking/AFNetworking

I am using AFNetworking classes for calling simple http web services please look at my example code,

#pragma mark - AFHTTPRequestOperationManager getter

-(AFHTTPRequestOperationManager *)manager
{
    if (!_manager)
    {
        _manager = [AFHTTPRequestOperationManager manager];
        _manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }

    return _manager;
}

#pragma mark - Web Api Methods

-(void)callMethodWithParameters:(NSDictionary *)parameters success:(RequestCompletionHandlerBlock)success failure:(RequestFailedHandlerBlock)failure
{
    [self.manager POST:BASE_URL parameters:parameters

               success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSError *parsingError;
         NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&parsingError];
         success(responseDict);
     }

               failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Error = %@",error);
         failure(error);
     }];
}

When I call the web services for the very first time I get the response what I expected but after calling it once I get the error message Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” but I don't get this message all the time sometimes I get correct response but sometimes I get the error message.

_Is this the problem of persistent connection?_
Please suggest what I can do here?

Most helpful comment

For what it's worth, I received this error 100% on Alamofire, when I sent an empty dictionary [:] on a GET request. The error could have been clearer than "The network connection was lost".

Hope this helps!

All 8 comments

Encountering the same issue.

_Greets! Actually I am facing the issue in all my devices iPhone 5s, iPhone 4, iPad Mini, and iPhone 6._

Please help

Duplicate of #2314.

Happening on the device. Not just the simulator.

Remove "keep-alive" from HTTP Header at server side.

For what it's worth, I received this error 100% on Alamofire, when I sent an empty dictionary [:] on a GET request. The error could have been clearer than "The network connection was lost".

Hope this helps!

Thank you @yujean! I spent an hour or two tearing my hair out over this issue, and your simple observation solved it for me.

Was this page helpful?
0 / 5 - 0 ratings