AFNetworking NSURLErrorDomain -1005

I use AFNetworking in the application and I am executing the following POST code.
Everything is fine (the request successfully communicates with the server), but when I call this method on a specific action that passes certain parameters (a small NSDictionary, with 4 keys and values), and only with this action I get a block failure 8 out of 10 times, and rarely do i get a success block. The problem becomes even more complicated, because even if I get a failure block, the server receives the passed parameters.

Why is this happening?

  + (void)postToServerWithParameters:(NSDictionary *)parameters
                           andPostPath:(ServerOperation)operation
                           andCallback:(serverResponse)callback
    {
        if (retryTimes <= 0) {
            callback(0, 0, @"error in posting, number of retries is finished", nil);
            retryTimes = 3;
            return;
        }

    NSString *postPath = [self choosePostPathByServerOperation:operation];
    NSURL *url = [NSURL URLWithString:BASE_URL];

    NSLog(@"parameters recieved:\n %@", parameters);
    NSLog(@"base url to post:\n %@", url.absoluteString);


    [clientRequest setObject:parameters];


    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];
    [client registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [client setParameterEncoding:AFJSONParameterEncoding];
    [client setDefaultHeader:@"Accept" value:@"application/json"];
    [client postPath:postPath
          parameters:[clientRequest dictionaryWithValuesForKeys:nil]

             success:^(AFHTTPRequestOperation *innerOperation, id responseObject) {

                 // init the retry counter
                 retryTimes = 3;

                 NSLog(@"post path: %@ response from AFNetworking: %@",postPath, responseObject);


                 int errorCode = [responseObject[@"mCode"] intValue];
                 BOOL isValidToken = [responseObject[@"mIsValidtoken"] boolValue];
                 NSString *msg = responseObject[@"mMessage"];
                 id object = responseObject[@"mData"];



                 // and if the callback is not nil,
                 // will will send the data to it.
                 if (callback) {
                     callback(errorCode, isValidToken, msg, object);

                 }
             }

             failure:^(AFHTTPRequestOperation *innerOperation, NSError *error) {

                 NSLog(@"AFclient:%@", client);

                 NSLog(@"request faild with AFNetworking\nerror: %@", error);
                 // handle comunication error with retries (till 3)
                 retryTimes--;
                 [self postToServerWithParameters:parameters andPostPath:operation andCallback:callback];

             }
     ];
}

mistake:

error: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x13423a80 {NSErrorFailingURLStringKey=http://www.some.url.com/something, NSErrorFailingURLKey=http://www.some.url.com/something, NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0xb9a7030 "The network connection was lost."}

solvable

The problem was server side. When performing a specific POST, the server performed an action in the background thread that caused the exception. This explains why POST succeeded and accidentally failed.

+4

Source: https://habr.com/ru/post/1524254/


All Articles