0

我试图通过确定下载固定大小的文件需要多少时间来计算网络的带宽。

starttime=Calendar.getInstance().getTimeInMillis();
HttpResponse response=cts.getContent();
if(response != null) 
   endtime=Calendar.getInstance().getTimeInMillis();    

这是我的getContent()方法:

public HttpResponse getContent()
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet postRequest = new HttpGet("myserverurl");
        try {
             Log.i(tag,"before execute");
             HttpResponse response = httpClient.execute(postRequest);
              Log.i(tag,"after execute");
            HttpEntity httpEntity = response.getEntity();
            is = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              Log.i(tag,sb.toString());
              response.toString();
              return response;

        } catch (Exception e) {
            Log.i(tag,"execption="+e);
            return null;
        }

    }

我计算此方法响应的结束时间。我的问题是何时是计算结束时间的正确时间?如果我在该行之后立即取结束时间:

HttpEntity httpEntity = response.getEntity();

我得到的带宽非常高,当我在 while 循环执行后这样做时,带宽完全不同。

哪种方法是正确的?文件何时完全下载?

4

1 回答 1

0

据我了解,它是同步的,与异步相反,所以在存储响应之后就是下载结束的地方

于 2013-05-31T17:44:23.653 回答