如何从BufferedReader获取百分比进度?

时间:2013-11-30 22:12:11

标签: java android apache-commons-httpclient

@Override
    protected StringBuilder doInBackground(String... url) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url[0]);
            HttpResponse response = client.execute(request);
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));
            long content = response.getEntity().getContentLength();
//Do not need 'String line'
            String line = "";
            StringBuilder htmlBuilder = new StringBuilder();
            long bytesRead = 0;
            while ((line = rd.readLine()) != null) {
                htmlBuilder.append(line);
                bytesRead = bytesRead + line.getBytes().length + 2;
                publishProgress(new Integer[]{(int) (((double) bytesRead / (double) content) * 100)});
            }
            return htmlBuilder;
        } catch (IOException e) {
            return null;
        }

    }

'long content'返回-1。这意味着内容大于长类的最大值。

尝试让它返回别的东西我试图扩展DefaultHttpClient =>

public class ImprovedHttpClient extends DefaultHttpClient implements HttpClient {
    @Override
public final HttpResponse execute(HttpUriRequest request)
        throws IOException, ClientProtocolException {
    // TODO Auto-generated method stub
    return super.execute(request);
}

}

上面的方法是最终的,所以我不能扩展它并使用多态...我坚持如何获得百分比进度,我不想使用另一个库。

为什么我必须实现HttpClient?根据apache文档,DefaultHttpClient已经实现了HttpClient;我必须实现它是没有意义的。但是,我可以看到eclipse如何阻止我找到它,因为最终方法无法扩展。

总之,如何在不返回-1的情况下从DefaultHttpClient获取内容的总字节数?

EDIT 这是从

打印出来的
System.out.println(Arrays.toString(response.getAllHeaders()));

Stackoverflow成功地使用此网址返回了百分比进度:("http://stackoverflow.com/questions/20306245/how-to-get-percentage-progress-from-a-bufferedreader#20306306");

[Cache-Control: public, max-age=33, Content-Type: text/html; charset=utf-8, Expires: Sat, 30 Nov 2013 22:37:14 GMT, Last-Modified: Sat, 30 Nov 2013 22:36:14 GMT, Vary: *, X-Frame-Options: SAMEORIGIN, Date: Sat, 30 Nov 2013 22:36:41 GMT, Content-Length: 51468, Via: HTTP/1.1 127.0.0.1:2020 (Brazil/2.0), Server: Brazil/2.0, Connection: Keep-Alive]

Google返回-1:     https://www.google.com/

[Date: Sat, 30 Nov 2013 22:41:44 GMT, Expires: -1, Cache-Control: private, max-age=0, Content-Type: text/html; charset=ISO-8859-1, Set-Cookie: PREF=ID=4c028dc2b639abc2:FF=0:TM=1385851304:LM=1385851304:S=we-TWKR-LtJcomHN; expires=Mon, 30-Nov-2015 22:41:44 GMT; path=/; domain=.google.com, Set-Cookie: NID=67=mId7x6boTt6BYcAWipXzM4Fjt_WGl7KQPgcHdPkA9yiOgHf8pTWl5k38AfFnA68NjL34rDRYsveh-QdLKcyoAzDZRigGe_5ydrwiELRSkW24Q0J7NdtErEnT93WXMjgM; expires=Sun, 01-Jun-2014 22:41:44 GMT; path=/; domain=.google.com; HttpOnly, P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info.", Server: gws, X-XSS-Protection: 1; mode=block, X-Frame-Options: SAMEORIGIN, Alternate-Protocol: 443:quic, Transfer-Encoding: chunked]

1 个答案:

答案 0 :(得分:1)

在某些情况下,HTTP客户端无法知道您获得的内容的大小。 HTTP标头有一个声明内容长度的位置,但这不是必需的。在服务器未指定内容长度的情况下,HTTPEntity返回-1表示长度。

在这种情况下,无法准确报告下载进度。你可以对内容长度进行“猜测”。或者,如果下载后有重要的处理,您可以下载整个内容,然后报告处理的进度。

检查服务器是否正在发送您正在测试的案例的内容长度。