2011-12-07 161 views
0

在google上搜索幾小時後,我仍然無法找到關於此問題的單個主題:
1.如果用戶沒有互聯網連接,我需要提供錯誤處理功能。
2.用戶有互聯網連接但無法連接到服務器。ios - 網絡/互聯網連接

我在哪裏可以放這些?我嘗試了一個主題,但沒有顯示警報視圖消息。

這裏是我的代碼:

- (IBAction)getDataPressed 
{ 
    if([myRequest_ isExecuting]) 
    { 
     return; 
    } 

    if(myRequest_ != nil) 
    { 
     [myRequest_ release]; 
    } 
    myRequest_ = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:URL_PATH]]; 
    myRequest_.delegate = self; 
    [myRequest_ startAsynchronous]; 
} 

#pragma ASI Delegate methods 
- (void)requestFinished:(ASIHTTPRequest *)request 
{  
    NSLog(@"Request finished successfully"); 
    NSLog(@"%@",[request responseString]); 

    NSDictionary *responseDictionary = [[request responseString]JSONValue]; 
    NSDictionary *arrayElement = [responseDictionary objectForKey:@"user"]; 

    NSString *ID = [arrayElement valueForKeyPath:@"id"]; 
    NSLog(@"id: %@",ID); 
    NSString *usr = [arrayElement valueForKeyPath:@"usr"]; 
    NSLog(@"usr: %@",usr); 
    NSString *gd = [arrayElement valueForKeyPath:@"gd"]; 
    NSLog(@"gd: %@",gd); 
    NSString *ag = [arrayElement valueForKeyPath:@"ag"]; 
    NSLog(@"ag: %@",ag); 
    NSString *st = [arrayElement valueForKeyPath:@"st"]; 
    NSLog(@"st: %@",st); 
    NSString *lf = [arrayElement valueForKeyPath:@"lf"]; 
    NSLog(@"lf: %@",lf); 
    NSString *da = [arrayElement valueForKeyPath:@"da"]; 
    NSLog(@"da: %d",da); 

    for(NSString *value in [arrayElement allValues]){ 
     NSLog(@"Found Value %@",value);  
     label.text = [value stringByAppendingFormat:@",%@,%@,%@,%@,%@,%@,",ID,usr,gd,ag,st,lf]; 

    } 
} 
- (void)requestFailed:(ASIHTTPRequest *)request 
{ 
    NSLog(@"Error %@", [request error]); 
    if([request error]) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Destination Unreachable" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 
} 
-(void) dealloc 
{ 
    [super dealloc]; 
} 

感謝那些誰可以幫助^ _^

回答

0

我想你可以嘗試Reachbility應用程序來幫助你,如果你有互聯網或不檢查。至於服務器本身,您可以使用NSUrlConnection Delegate方法來檢查您的請求是否存在問題(通過查看即將發佈的HTTP代碼的類型)

+0

即使使用ASIHTTPREQUEST即時使用NSURLConnection也可以嗎? – NoobMe

+0

我認爲它應該以相同的方式工作(我也使用ASIHTTPRequest)。我認爲他們有兩種方法,一種是成功的,另一種是失敗的。儘管我在成功方法上收到HTTP響應代碼400。 – Peres

+0

可以請你解釋它更多先生..和哦,順便說一句,當我試圖在可達性執行之前,即使在控制檯甚至不給予迴應....如何 – NoobMe