2009-02-27 87 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