2016-05-25 46 views
0

我有這樣的代碼:AFNetworking - 加載數據,但錯誤504發生

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調用。主要是它工作得很好,但有時我得到這個錯誤:

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...

然而,在控制檯上我看到「正在進行:100%」。另外,在瀏覽器中,我總是獲取所有數據。因此,我建議,這是我的代碼問題。我該如何解決這個問題?

回答

0

的504網關超時錯誤是一個HTTP狀態代碼是指一個服務器未收到來自試圖加載網頁或填充由瀏覽器另一個請求,它正在訪問其他服務器及時響應。

更多在這裏閱讀:http://pcsupport.about.com/od/findbyerrormessage/a/504error.htm

所以沒有什麼錯在你的代碼。

+0

但是你知道,爲什麼我仍然得到100%的完井? –

+0

@AivanF。我沒有明確的解釋,但嘗試序列化,因爲錯誤顯示error.serialization。 請參考以下序列化:https://github.com/AFNetworking/AFNetworking –

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

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

我只是AFNetworking的新手,你能解釋一下如何將這個代碼與我的混合? –

+0

@AivanF。您可以通過使用此代碼** ** ASIHTTPRequest LIB [此處鏈接](https://github.com/pokeb/asi-http-request)在這之後看到我更新的代碼將數據發送到服務器.. 有注意:親愛的你必須在實現之前理解代碼。 – vaibhav

相關問題