2009-02-27 43 views
0

我正在使用Apple文檔來創建應用程序。我成功連接到服務器,但是我從服務器接收到0個字節(無響應數據)。我採取以下步驟:從iPhone上的URL接收數據?

  1. 我創建了一個基於視圖的應用並添加屬性 'receivedData':

    在ViewController.h:

    @property (nonatomic, retain) NSMutableData *receivedData;

    在ViewController.m:

    @synthesize receivedData;
  2. ViewController.m的動作'ViewDidLoad',我加:

    receivedData = [NSMutableData alloc];
  3. 在視圖中添加一個按鈕,併爲其添加動作:

    // 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字節。關於什麼錯誤的任何想法?我的代碼的簡單修改將不勝感激。

回答

0

您的代碼只創建HTTP連接 - 在框架調用委託回調後(只要收到HTTP響應),數據將只寫入並存在於receivedData中。您可以從Apple's documentation

+0

謝謝。添加了'didReceiveData'的方法,併成功從服務器接收數據。 – 2009-02-27 09:10:18