0

我有这个代码:

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。大多数情况下它运行良好,但有时我会收到此错误:

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

但是,在控制台上我看到"In progress: 100%"。此外,在浏览器中,我总是可以获取所有数据。因此,我建议,这是我的代码的问题。我该如何解决这个问题?

4

2 回答 2

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];
于 2016-05-25T07:40:35.467 回答
0

504 Gateway Timeout 错误是一个 HTTP 状态代码,这意味着一台服务器在尝试加载网页或填充浏览器的另一个请求时没有收到来自它正在访问的另一台服务器的及时响应。

在这里阅读更多:http: //pcsupport.about.com/od/findbyerrormessage/a/504error.htm

所以你的代码没有错。

于 2016-05-25T07:21:45.180 回答