2012-04-18 56 views
0

我已經看過NSURLConnectionDelegate connection:didReceiveData not working已經數據,但似乎沒有從任何好的結果,所以我很好奇,爲什麼我不能得到任何數據。NSUrlConnectionDelegate不調用方法來加載

我把斷點didReceiveResponsedidReceiveData

它打印出「連接成功」,所以我知道連接已啓動。

我正在使用ARC進行內存管理。

- (void)load { 
     request = [NSMutableURLRequest requestWithURL:myURL 
                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                 timeoutInterval:60]; 

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if (conn) { 
     [conn start]; 
     NSLog(@"connection succeeded, %s", [myURL description]); 
     responseData = [NSMutableData data]; 
    } else { 
     NSLog(@"connection failed"); 
    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    responseData = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
} 

UPDATE:

,看我怎麼在Asynchronous unit test not being called by SenTestCase測試這個樣子。

我沒有落實jonkroll提到的兩種方法,在他的回答,我只是沒有告訴他們,但他們也沒有被調用。

我又增加了[conn start]只是因爲它不工作,我希望可以解決這個問題,但沒有這樣的運氣。

回答

3

當你宣佈你的連接是這樣的:

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

您正在創建一個本地指針。當你的方法完成時,因爲它是NSURLConnection的最後一個強烈參考,所以ARC將發佈它。您需要使用強大的伊娃(和/或)財產來持有對您創建的NSURLConnection的強烈參考。

編輯

下面是我在一個示例項目測試代碼的基本樣品。試試看。詳細日誌幫助。

@implementation <#Your class here#> { 
    // With ARC ivars are strong by default 
    NSMutableData *_downloadedData; 
    NSURLConnection *_connection; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    NSHTTPURLResponse *realResponse = (NSHTTPURLResponse *)response; 
    if (realResponse.statusCode == 200){ 
     // Really any 2** but for example 
     _downloadedData = [[NSMutableData alloc] init]; 
     NSLog(@"Good response"); 
    } else { 
     NSLog(@"Bad response = %i",realResponse.statusCode); 
    } 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
    if (connection == _connection){ 
     [_downloadedData appendData:data]; 
     NSLog(@"Getting data..."); 
    } 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    if (connection == _connection){ 
     _connection = nil; 
     NSLog(@"We're done, inform the UI or the delegates"); 
    } 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
    _connection = nil; 
    NSLog(@"Oh no! Error:%@",error.localizedDescription); 
} 
- (void)load { 
    NSURL *url = [NSURL URLWithString:@"http://www.google.com/"]; 
    NSURLRequest *request = [NSMutableURLRequest requestWithURL:url 
             cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
            timeoutInterval:60]; 
    // Assign strong pointer to new connection 
    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    NSLog(@"Connection was initialized? = %@",(!!_connection)[email protected]"YES":@"NO"); 
} 
@end 
+0

我改變了conn爲:@property NSURLConnection * conn;但是我得到相同的結果,沒有收到數據。等待超過90秒後,我的單元測試停止等待並失敗。 – 2012-04-18 02:12:36

+0

@JamesBlack我加了一些測試過的示例代碼。 – NJones 2012-04-18 03:10:18

+0

我還不確定它的區別,但現在正在工作,謝謝你。 – 2012-04-19 00:17:33

0

的NSURLConnection的方法initWithRequest開始用於從URL數據的異步請求。由於請求是異步完成的,因此不能期望在調用請求的相同方法中使用響應。相反,您需要在NSURLConnection的委託回調方法中這樣做。您已經實施了didReceiveResponse:didReceiveData:,但還有其他一些對您有用的其他內容。

如果你想看看響應的內容,你應該這樣做在connectionDidFinishLoading:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // response is complete, do something with the data 
    NSLog(@"%@", responseData); 
} 

事實上,你的代碼打印出「連接成功」並不真正意味着該請求是成功的,只有NSURLConnection對象已成功創建。爲了測試是否有與連接有問題,可以實現委託方法connection:didFailWithError:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Connection failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
} 

也沒有必要調用[conn start]。請求將在您致電時自動啓動initWithRequest:

我建議您閱讀關於Using NSURLConnection的Apple文檔以瞭解更多詳細信息。

+0

謝謝。根據我的日誌記錄,我添加了[conn start],因爲它不起作用,我也實施了其他方法,但是,再一次,他們都沒有做任何事情。 – 2012-04-18 01:33:20