2012-06-07 71 views
1

我在NSURLConnection有問題。我的didRecieveData方法沒有被調用。我不知道有什麼問題。我也經歷了幾乎所有以前的問題,但似乎沒有解決。連接:didReceiveData不叫

請幫我一把。 這是我的代碼。

 -(void)gettheJSONdata 
    { 
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_serverAddress cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; 

      [request setHTTPMethod: @"GET"]; 

      NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 

      NSLog(@"This is displayed"); 
    } 

    -(void)connection:(NSURLConnection*)connection didReceiveData:(NSData *)data 
    { 
      NSLog(@"This is not displayed."); 
      AppAppDelegate *JSONdata = (AppAppDelegate*)[[UIApplication sharedApplication] delegate]; 

      JSONdata.userloginJSONdata = [[NSData alloc] initWithData:data]; 

      NSError *error; 

      _result = [NSJSONSerialization JSONObjectWithData:JSONdata.userloginJSONdata options:NSJSONReadingMutableContainers error: &error]; 

      NSString *test = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
      NSLog(@"Json data : %@",test); 
    } 

    -(void) connectionDidFinishLoading:(NSURLConnection*) connection 
    { 
      AppAppDelegate *resultofJSONdata = (AppAppDelegate*)[[UIApplication sharedApplication] delegate]; 

      resultofJSONdata.JSONdataresult = _result; 
     } 
+0

到你頭文件你加'NSURLConnectionDelegate'協議? –

+0

是... @interface主幹:NSObject 這樣... – khushalbokadey

+0

頭文件中的協議不會影響任何內容。真正的問題是,是否是NSURLConnection的代表?我假設它是...因爲我猜測connectionDidFinishedLoading:被擊中,對吧? – Christian

回答

0

最後我解決了這個問題。 我沒有合成JSONdataresult。

我沒有意識到這可能會導致NSURLConnection的問題。

0

您沒有開始請求。您需要:

[connection start]; 

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 

編輯:我在下面的評論有不正確的方法名稱。嘗試實現兩個:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

看看是否無論這些都被擊中。

+0

這也不是問題... 嘗試但結果相同。 – khushalbokadey

+1

用新代碼更新你的文章,因爲這肯定是你的代碼有問題。還要實現connectionDidStart:delegate方法,並檢查是否命中。因爲您的請求可能失敗,這不會影響您發佈的任何方法。 – Christian

+0

有沒有像connectionDidStart這樣的方法:??? 我從來沒有見過它。 你可以給我的語法.. – khushalbokadey

0

在我的情況下頭球攻門被無連接didReceiveData沒有得到所謂的,可以肯定的,如果它需要請求補充授權,

[urlRequest setValue:[UserContext sharedInstance].requesthandler.reqHeader forHTTPHeaderField:@"Authorization"];