我试图通过确定下载固定大小的文件需要多少时间来计算网络的带宽。
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 循环执行后这样做时,带宽完全不同。
哪种方法是正确的?文件何时完全下载?