本文介绍了didReceiveData不能获取所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSURLConnection下载JSON,但是除非我强迫应用暂停几秒钟,否则我获得的数据将不完整.它总是大约2600字节,我的响应应该在70000左右.

I am trying to download a JSON with NSURLConnection, but unless I force the app to pause some seconds the data I get isn't complete. It is always around 2600 bytes and my response should be around 70000.

任何线索为什么会发生这种情况?

Any clue why is this happening?

谢谢

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    _responseData = [[NSMutableData alloc] init];
    //sleep(10);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_responseData appendData:data];
    [self getDataJSON: _responseData];
}

推荐答案

didReceiveData在接收数据时被调用很多次

didReceiveData is called a lot of times, while it is receiving data

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_responseData appendData:data];

}

您必须等待,直到它完成数据接收

You have to wait until it finish receiving data

- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
     [self getDataJSON: _responseData];
}

这篇关于didReceiveData不能获取所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:23