我正在使用Apple文檔來創建應用程序。我成功連接到服務器,但是我從服務器接收到0個字節(無響應數據)。我採取以下步驟:從iPhone上的URL接收數據?
我創建了一個基於視圖的應用並添加屬性 'receivedData':
在ViewController.h:
@property (nonatomic, retain) NSMutableData *receivedData;
在ViewController.m:
@synthesize receivedData;
ViewController.m的動作'ViewDidLoad',我加:
receivedData = [NSMutableData alloc];
在視圖中添加一個按鈕,併爲其添加動作:
// create the request NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://..."] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; // create the connection with the request // and start loading the data NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { // Create the NSMutableData that will hold // the received data // receivedData is declared as a method instance elsewhere receivedData=[[NSMutableData data] retain]; } else { // inform the user that the download could not be made }
當我調試這些代碼,我發現receivedData返回0字節。關於什麼錯誤的任何想法?我的代碼的簡單修改將不勝感激。
謝謝。添加了'didReceiveData'的方法,併成功從服務器接收數據。 – 2009-02-27 09:10:18