2015-09-08 62 views
0

我正在嘗試訪問Web服務。所有作品都很好。但是如果服務器不工作,那麼我的應用程序崩潰。 如何處理無服務器響應。 請幫忙如何檢查目標中是否沒有服務器響應c

這是我打網頁服務的代碼。

NSMutableDictionary *get = [[NSMutableDictionary alloc]init]; 
[get setObject:@"0" forKey:@"unit"]; 
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:get options:kNilOptions error:nil]; 
NSString *jsonInputString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
NSString *post = [[NSString alloc]initWithFormat:@"req=%@",jsonInputString]; 

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",getCommunity]]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

NSError *error; 
NSURLResponse *response; 
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
if (responseData != nil) { 
    NSDictionary *jsonRecieveDict = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 
    NSLog(@"jsonArray =======%@",jsonRecieveDict); 
} 
if (error) 
{ 
    UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Servor not responding" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [errorAlert show]; 
} 

**錯誤是因爲無效的狀態代碼從服務器**作者

+0

如果服務器沒有響應,那麼你的服務器沒有響應就會在'error'中被捕獲。關於上面的代碼,似乎是什麼問題? –

+0

@SahebRoy - 它在沒有互聯網的情況下工作,但如果服務器關閉,應用程序崩潰。問題是**服務器的狀態代碼** –

回答

2
if (error != nil) { 
    // Something went wrong... 
    NSLog(@"Servor not responding %@",error.description); 
    return; 
} 
if ([response statusCode] >= 300) { 
    NSLog(@"Servor not responding, status code: %ld", (long)[response statusCode]); 
    return; 
} 

第一條件應該是錯誤校驗,如果第二響應來檢查狀態代碼,然後只執行剩餘操作

也改變NSURLResponse *response;NSHTTPURLResponse *response

或區分執行

NSMutableDictionary *get = [[NSMutableDictionary alloc]init]; 
[get setObject:@"0" forKey:@"unit"]; 

if([NSJSONSerialization isValidJSONObject:get]){ 


//convert object to data 
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:kNilOptions error:nil]; 


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:@"your url"]]; 


[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setHTTPBody:jsonData]; 


NSURLSessionConfiguration *config=[NSURLSessionConfiguration defaultSessionConfiguration]; 

NSURLSession *session=[NSURLSession sessionWithConfiguration:config]; 

NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 

    if(response){ 
     NSString *resp = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding]; 
     NSLog(@"Echo %@",resp); 


    } 

    else{ 
     NSLog(@"Timeout"); 

    } 



}]; 
[task resume]; 


} 
+0

響應狀態代碼給我看沒有可見的接口爲NSURLRESPONSE聲明選擇器狀態代碼 –

+0

請在我的代碼中更新爲編程中的新增 –

+0

加載然後顯示「服務器未響應 」。正如我給連接時間輸出20秒。 –

相關問題