iOS:TouchJSON&;检索到的数据大小
我想显示一个UIProgressView,指示使用touchJSON请求JSON数据时接收的数据量。我想知道是否有办法监听接收到的数据的大小 我使用以下方式请求数据:iOS:TouchJSON&;检索到的数据大小,ios,sync,touchjson,Ios,Sync,Touchjson,我想显示一个UIProgressView,指示使用touchJSON请求JSON数据时接收的数据量。我想知道是否有办法监听接收到的数据的大小 我使用以下方式请求数据: - (NSDictionary *)requestData { NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:apiURL]]; NSError *error =
- (NSDictionary *)requestData
{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:apiURL]];
NSError *error = nil;
NSDictionary *result = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error];
if(error != NULL)
NSLog(@"Error: %@", error);
return result;
}
您需要引入更多的代码来包含下载状态指示条。此时,您可以使用
[NSData dataWithConentsOfURL:…]下载数据。相反,您将创建一个类,该类使用NSURLConnection
对象下载数据,将该数据累积到MSMutableData对象中,并相应地更新UI。您应该能够使用ContentLength
HTTP头和-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据代码>更新以确定下载的状态
以下是一些相关的方法:
- (void) startDownload
{
downloadedData = [[NSMutableData alloc] init];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
- (void)connection:(NSURLConnection *)c didReceiveResponse:(NSURLResponse *)response
{
totalBytes = [response expectedContentLength];
}
// assume you have an NSMutableData instance variable named downloadedData
- (void)connection:(NSURLConnection *)c didReceiveData:(NSData *)data
{
[downloadedData appendData: data];
float proportionSoFar = (float)[downloadedData length] / (float)totalBytes;
// update UI with proportionSoFar
}
- (void)connection:(NSURLConnection *)c didFailWithError:(NSError *)error
{
[connection release];
connection = nil;
// handle failure
}
- (void)connectionDidFinishLoading:(NSURLConnection *)c
{
[connection release];
connection = nil;
// handle data upon success
}
就我个人而言,我认为最简单的方法是创建一个类,该类实现上述方法来进行一般数据下载并与该类进行接口
这应该足以满足您的需要。您需要引入更多的代码来包含下载状态指示条。此时,您可以使用[NSData dataWithConentsOfURL:…]下载数据。相反,您将创建一个类,该类使用NSURLConnection
对象下载数据,将该数据累积到MSMutableData对象中,并相应地更新UI。您应该能够使用ContentLength
HTTP头和-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据代码>更新以确定下载的状态
以下是一些相关的方法:
- (void) startDownload
{
downloadedData = [[NSMutableData alloc] init];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
- (void)connection:(NSURLConnection *)c didReceiveResponse:(NSURLResponse *)response
{
totalBytes = [response expectedContentLength];
}
// assume you have an NSMutableData instance variable named downloadedData
- (void)connection:(NSURLConnection *)c didReceiveData:(NSData *)data
{
[downloadedData appendData: data];
float proportionSoFar = (float)[downloadedData length] / (float)totalBytes;
// update UI with proportionSoFar
}
- (void)connection:(NSURLConnection *)c didFailWithError:(NSError *)error
{
[connection release];
connection = nil;
// handle failure
}
- (void)connectionDidFinishLoading:(NSURLConnection *)c
{
[connection release];
connection = nil;
// handle data upon success
}
就我个人而言,我认为最简单的方法是创建一个类,该类实现上述方法来进行一般数据下载并与该类进行接口
这应该足以满足你的需要