AFNetworking - 加载数据,但发生错误504

时间:2016-05-25 07:14:29

标签: ios objective-c networking afnetworking afnetworking-3

我有这段代码:

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

viewDidLoad调用。大多数情况下效果很好,但有时我会收到此错误:

  

错误Domain = com.alamofire.error.serialization.response Code = -1011“请求失败:网关超时(504)”UserInfo = {NSUnderlyingError = 0x7fdd40c18aa0 {Error Domain = com.alamofire.error.serialization.response Code = -1016“请求失败:不可接受的内容类型:text / html”......等等,没有什么有趣的......

但是,在控制台我看到“正在进行中:100%”。另外,在浏览器中我总是得到所有数据。因此,我建议,这是我的代码的问题。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

你可以通过下面的代码来处理这些错误.. :)

- (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;
        }
    }
}
  

发布请求

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

答案 1 :(得分:0)

504网关超时错误是一个HTTP状态代码,这意味着一个服务器在尝试加载网页或浏览器填写另一个请求时没有从其访问的另一台服务器收到及时响应。

在此处阅读更多内容:http://pcsupport.about.com/od/findbyerrormessage/a/504error.htm

你的代码没有错。