我在植入简单的NSURLConnection时遇到了这个奇怪的问题...
方法didReceiveData得到调用,我很高兴尝试追加接收数据,但是... nada!
确实存在一些数据(因为长度表示但是appendData不附加数据!
我开始对这个问题感到头疼,在迟到之前我需要一些帮助: - )
这里有一些代码可供参考:
我的标题......
@interface ActionViewController : UITableViewController {
Site *site;
NSURLConnection *siteConnection;
NSMutableData *receivedData;
UIView *waitView;
UIActivityIndicatorView *activityIndicator;
int nConnections;
BOOL fail;
}
我的实施..
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
NSLog(@"Received %d bytes of data",[data length]);
[receivedData appendData:data];
NSLog(@"Received %d bytes of data",[receivedData length]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[activityIndicator stopAnimating];
waitView.hidden = YES;
// release the connection
[connection release];
}
控制台输出......
[Session started at 2010-08-21 21:27:55 -0400.]
2010-08-21 21:28:19.263 myApp[2042:207] Received 108 bytes of data
2010-08-21 21:28:19.263 myApp[2042:207] Received 0 bytes of data
2010-08-21 21:28:19.263 myApp[2042:207] Succeeded! Received 0 bytes of data
我不明白! HELP !!!
BTW,数据是一个简单的xml结果,看起来像这样......<donnee>0</donnee><donnee>0</donnee><donnee>0</donnee><donnee>1</donnee><donnee>0</donnee><donnee>0</donnee>
答案 0 :(得分:12)
好吧,在本网站的另一个问题中得到了我的回答NSMutableData ...忘了初始化这个东西! (代码18或将消息发送到nil的剩余部分是Objective-C的NICE特征)
receivedData = [[NSMutableData alloc] init];