我正在使用Apple Document创建app.I成功连接到服务器,但我从服务器收到0个字节(没有响应数据)。我采取以下步骤:
我创建了一个基于视图的应用程序并添加了一个属性'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个字节。关于出了什么问题的任何想法?我们将非常感谢我的代码的简单修改。
答案 0 :(得分:0)
您的代码只创建HTTP连接 - 只有在框架调用委托回调后(一旦收到HTTP响应),数据才会被写入并在receivedData中可用。您可以从Apple's documentation
获取更多信息和示例代码答案 1 :(得分:0)
答案与上次我在How can I receive data from URL on iPhone?时为你回答时的答案相同。我给出了详细的解释 - 你读过它吗?