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

任何线索为什么会这样?

谢谢

- (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在接收数据时被多次调用

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

}

您必须等到它完成接收数据
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
     [self getDataJSON: _responseData];
}

关于ios - didReceiveData没有获取所有数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20095234/

10-13 03:59