2013-08-06 2 views
4

내 앱에서 웹 서비스를 구현합니다. 내 방식은 전형적입니다.인터넷에 연결되지 않은 상태에서 NSJSONSerialization의 충돌을 처리하는 방법

- (BOOL)application:(UIApplication *)application  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
//Web Service xxx,yyy are not true data 
    NSString *urlString = @"http://xxx.byethost17.com/yyy"; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    dispatch_async(kBackGroudQueue, ^{ 
     NSData* data = [NSData dataWithContentsOfURL: url]; 
     [self performSelectorOnMainThread:@selector(receiveLatest:)  withObject:data waitUntilDone:YES]; 
    }); 
    return YES; 
} 

- (void)receiveLatest:(NSData *)responseData { 
    //parse out the json data 
    NSError* error; 
    NSDictionary* json = [NSJSONSerialization 
         JSONObjectWithData:responseData 
         options:kNilOptions 
         error:&error]; 
    NSString *Draw_539 = [json objectForKey:@"Draw_539"]; 
.... 

콘솔 오류 메시지 : 응용 프로그램을 종료

* 때문에, 이유 캐치되지 않는 예외 'NSInvalidArgumentException'에 '데이터 매개 변수는 전무하다'

내 아이폰이 연결이

인터넷에, 응용 프로그램이 성공적으로 작동합니다. 하지만 인터넷 연결이 끊어지면 앱이 충돌합니다. NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 이 오류를 어떻게 처리 할 수 ​​있습니까? NSError은 도움이 되었습니까?

+0

"responseData"가 nil이라고 오류가 표시됩니다. 예외를 피하는 방법은 "responseData"를 테스트하고 그것이 없으면 JSONObjectWithData를 호출하지 않는 것입니다. 대신 그러나 당신은 당신이이 오류 상태에 대해해야한다고 생각합니다. –

+0

시도/catch, 또는 오류가 오류인지 확인하십시오. –

+0

@HotLicks 감사합니다. if 문'if (responseData)'를 추가합니다. 그러면 responseData가 nil이면 JSONObjectWithData가 호출되지 않습니다. 내 질문은 일반적인 항목 수준이라고 생각하지만 웹에서 정보를 찾을 수 없습니다. –

답변

11

"responseData"가 nil이라는 오류가 발생했습니다. 예외를 피하는 방법은 "responseData"를 테스트하고 그것이 없으면 JSONObjectWithData를 호출하지 않는 것입니다. 대신 그러나 당신은 당신이이 오류 상태에 대해해야한다고 생각합니다.

+0

"responseData"가 nil인지 정상적으로 확인하는 방법에 대한 제안 사항이 있으십니까? –

9

메서드로 전달하기 전에 responseData이 nil인지 여부는 확인하지 않았습니다.

아마 당신이 시도해야합니다

- (void)receiveLatest:(NSData *)responseData { 
    //parse out the json data 
    NSError* error; 
    if(responseData != nil) 
    { 
     NSDictionary* json = [NSJSONSerialization 
         JSONObjectWithData:responseData 
         options:kNilOptions 
         error:&error]; 
     NSString *Draw_539 = [json objectForKey:@"Draw_539"]; 
    } 
    else 
    { 
     //Handle error or alert user here 
    } 
    .... 
} 

편집-1 : JSON 데이터가 성공적으로있는 NSDictionary 여부로 변환하면 좋은 연습, 당신은 확인하고 볼 수 JSONObjectWithData:options:error: 방법 후이 error 개체를 확인해야합니다

- (void)receiveLatest:(NSData *)responseData { 
    //parse out the json data 
    NSError* error; 
    if(responseData != nil) 
    { 
     NSDictionary* json = [NSJSONSerialization 
         JSONObjectWithData:responseData 
         options:kNilOptions 
         error:&error]; 
     if(!error) 
     { 
       NSString *Draw_539 = [json objectForKey:@"Draw_539"]; 
     } 
     else 
     { 
       NSLog(@"Error: %@", [error localizedDescription]); 
       //Do additional data manipulation or handling work here. 
     } 
    } 
    else 
    { 
     //Handle error or alert user here 
    } 
    .... 
} 

이렇게하면 문제가 해결됩니다.

도움이 더 필요하면 알려주세요.

관련 문제