0

我正在为我的服务器创建一个 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]);
}
4

2 回答 2

3

responseData 未初始化:)

于 2013-02-04T21:20:24.320 回答
1

典型的!就在我完成发布问题时,答案突然出现在我身上。我从来没有初始化过responseData。所以在viewDidLoad我添加了下面的行:

- (void)viewDidLoad
{

  [super viewDidLoad];

  responseData = [NSMutableData data];

}
于 2013-02-04T21:17:41.543 回答