Strange problem with appendData in didReceiveData method of NSURLConnection

跟風遠走 提交于 2019-12-08 01:18:11

问题


I got this weird problem in implanting simple NSURLConnection...

The method didReceiveData get call and I'm happily trying to append the receive data but... nada!

There's some data for sure (as the length indicate but appendData do NOT append the data!

I start to bang my head on this one and I need some help before it's to late :-)

Here some code to look at :

My header...

@interface ActionViewController : UITableViewController {
 Site *site;

 NSURLConnection *siteConnection;
 NSMutableData *receivedData;
    UIView *waitView;
    UIActivityIndicatorView *activityIndicator;

 int nConnections;
    BOOL fail;
}

My implementation..

- (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];
}

The console output...

[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

I don't get it! HELP!!!

BTW, the data is a simple xml result that look like this...

<donnee>0</donnee><donnee>0</donnee><donnee>0</donnee><donnee>1</donnee><donnee>0</donnee><donnee>0</donnee>

回答1:


Well, got my answer in an another question on this site about NSMutableData... forgot to initialize the thing! (code 18 or a remainder of sending message to nil is a NICE feature of Objective-C)

receivedData = [[NSMutableData alloc] init];


来源:https://stackoverflow.com/questions/3539745/strange-problem-with-appenddata-in-didreceivedata-method-of-nsurlconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!