【问题标题】:NSURLConnection Receiving Data in PercentageNSURLConnection 接收数据百分比
【发布时间】:2015-02-20 04:18:44
【问题描述】:

我有一个简单的 NSURLConnection,我想获取解析数据的进度百分比。那就是加载的百分比。是否可以计算。 这个我试过了,

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    urlData = [NSMutableData data];
    [urlData setLength:0];
  expectedBytes = [response expectedContentLength];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
float progressive = (float)[urlData length] / (float)expectedBytes;
}

这里,expectedBytes 正在获取 -1。 json 链接正在获取 jsonResult。 让我知道如何计算加载进度。

【问题讨论】:

  • 它必须从服务器发送以确定预期长度。也许this 会帮助你。

标签: ios objective-c nsurlconnection


【解决方案1】:

使用下面的代码。这将为您工作,简单易行。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{
      urlData = [NSMutableData data];
      [urlData setLength:0];
      double expectedBytes = [[[response allHeaderFields] objectForKey:@"Content-Length"] doubleValue];
    }

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
      float progressive = (float)[urlData length] / (float)expectedBytes;
}

【讨论】:

  • 您应该将“-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response”更改为“-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)响应“以使这项工作
  • 错误:没有可见的 NSURLConnection 的@interface。
  • 在 didRecieveResponse 方法中将 NSURLConnection 改为 NSUHTTPRLConnection
猜你喜欢
  • 1970-01-01
  • 2014-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-21
相关资源
最近更新 更多