0
votes

I have this code:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

NSDictionary *parameters = @{@"client_id": some index};

[manager GET:@"some address"
  parameters:parameters
    progress:^(NSProgress * _Nonnull progress) {
        NSLog(@"In progress: %d%%", (int)([progress fractionCompleted] * 100 ));
    }
     success:^(NSURLSessionTask *task, id responseObject) {
         [self processData:[responseObject objectForKey:@"orders"]];
     }
     failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"%@", error);
     }];

It is called from viewDidLoad. Mostly it works well, but at times I get this error:

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: gateway timed out (504)" UserInfo={NSUnderlyingError=0x7fdd40c18aa0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" ...etc, nothing interesting...

However, at the console I see "In progress: 100%". In addition, in a browser I always get all the data. Hence, I propose, it is a problem of my code. How can I solve this?

2

2 Answers

1
votes

you can handle those errors kindly go through the below code .. :)

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *str = [request responseString];
    NSLog(@"%@",str);
    NSMutableDictionary *dictjson = [str JSONValue];

    NSInteger iStatus = [[dictjson objectForKey:@"status"] integerValue];
    if (iStatus == 200)
    {
        //        NSMutableDictionary *dictuser = [dictjson objectForKey:@"userinfo"];
        //        [AppDelegate showMessage:@"Register successed" withTitle:@"Success"];
        [self performSegueWithIdentifier:@"sw_right" sender:nil];
    }
    else
    {
        switch (iStatus)
        {
            case 1000:
                [AppDelegate showMessage:@"Packet no field" withTitle:@"Error"];
                return;
                break;
            case 600:
                [AppDelegate showMessage:@"Email duplicated" withTitle:@"Error"];
                return;
                break;
            case 606:
                [AppDelegate showMessage:@"Register failed, Please try again." withTitle:@"Error"];
                return;
                break;

            default:
                [AppDelegate showMessage:@"Register failed, Please try again." withTitle:@"Error"];
                return;
                break;
        }
    }
}

post request

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://yourApiURL"]]];

    [request setPostValue:strFirstName   forKey:@"fname"];
    [request setPostValue:strLastName    forKey:@"lname"];
    [request setPostValue:strUniversity  forKey:@"university"];
    // add post parameters so on

    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"Accept" value:@"application/json"];
    [request setDelegate:self];
    [request setTimeOutSeconds:30.0];
    [request setRequestMethod:@"POST"];
    [request startAsynchronous];
0
votes

The 504 Gateway Timeout error is an HTTP status code that means that one server did not receive a timely response from another server that it was accessing while attempting to load the web page or fill another request by the browser.

Read more here : http://pcsupport.about.com/od/findbyerrormessage/a/504error.htm

So nothing wrong in your code.