我正在向我的服务器进行NSURLConnection但responseData
在connectionDidFinishLoading
中最终为空,我无法弄清楚原因。这是连接的代码:
NSMutableData *responseData;
- (void)myFunction:(id)sender {
NSString *url = @"http://www.example.com/";
NSURL *URL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
// NSURLConnection Delegates
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
} // end connectionDidFinishLoading
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection error: %@",[error description]);
}
答案 0 :(得分:3)
responseData未初始化:)
答案 1 :(得分:1)
典型!就在我完成发布问题时,答案就在我面前。我从未初始化responseData
。所以在viewDidLoad
我添加了以下一行:
- (void)viewDidLoad
{
[super viewDidLoad];
responseData = [NSMutableData data];
}