0

因此,這裏是我的問題:NSURL連接會/不會加載數據

我跟着iPhone開發者文檔幾乎到T的NSURLConnection tutorial,它只是有點工作。

這裏就是這一切都錯了:
對象似乎是正確的,並委託給connectionDidFinishLoading創建的,但任何URL我嘗試加載的響應數據最終總是隻爲0字節。如果這有什麼不同,我正在模擬器中運行。

這裏是我的相關代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.title = @"WVFS Player"; 

    //create a request 
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wvfs.josh-kaplan.com/nowPlaying.php"] 
               cachePolicy:NSURLRequestUseProtocolCachePolicy 
              timeoutInterval:60.0]; 
    // create a connection 
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    if(theConnection) { 
     // create the datum 
     responseData=[[NSMutableData data] retain]; 
    } else { 
     // code this later 
    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [responseData setLength:0]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // make it work 
    NSLog(@"Succeeded! Received %d bytes of data:",[responseData length]); 

    // release it 
    [connection release]; 
    [responseData release]; 
} 

這是我的日誌輸出:

[Session started at 2010-03-14 09:01:09 -0400.] 
2010-03-14 09:01:14.784 WVFS[19571:207] Succeeded! Received 0 bytes of data: 

任何想法?

回答

4

你忘了執行connection:didReceiveData:

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