Android 检查 post 请求的速度
Android Check how fast post request took
我正在尝试检查 Post 请求的执行速度。我正在 AsyncTask 中执行请求。
这就是我目前检查执行时间的方式
long start = System.nanoTime();
new POSTTask().execute();
long end = System.nanoTime() - start;
我不确定这个给出的时间是否是Post请求的实际时间。在 AsyncTask 中,我唯一要做的就是以下
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> postParameters = new ArrayList<>();
postParameters.add(new BasicNameValuePair("test", "tets1"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = httpClient.execute(request);
result = EntityUtils.toString(response.getEntity());
Log.i("Result", result);
} catch (IOException e) {
e.printStackTrace();
}
我的做法是正确的还是错误的?
不,这不是实际时间。您正在尝试在主线程上获取执行时间,但您的 post 查询在后台的另一个线程中 运行。
这样使用
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> postParameters = new ArrayList<>();
postParameters.add(new BasicNameValuePair("test", "tets1"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
long start = System.nanoTime();
HttpResponse response = httpClient.execute(request);
long end = System.nanoTime() - start;
// Print value of end here
result = EntityUtils.toString(response.getEntity());
Log.i("Result", result);
} catch (IOException e) {
e.printStackTrace();
}
不对,这是错误的。在执行请求之前,AsyncTask
returns 上的 execute
方法立即生效。
如果将计时代码移到 AsyncTask
中,特别是将其放在这一行周围
HttpResponse response = httpClient.execute(request);
您将获得实际时间。
这是错误的,因为这些行没有按顺序执行。
new POSTTask().execute();
启动一个新线程,这意味着您的 2 个时间戳将非常接近,相隔几毫秒,这是不准确的
你应该在 doInBackground
的所有时间里做什么
我正在尝试检查 Post 请求的执行速度。我正在 AsyncTask 中执行请求。
这就是我目前检查执行时间的方式
long start = System.nanoTime();
new POSTTask().execute();
long end = System.nanoTime() - start;
我不确定这个给出的时间是否是Post请求的实际时间。在 AsyncTask 中,我唯一要做的就是以下
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> postParameters = new ArrayList<>();
postParameters.add(new BasicNameValuePair("test", "tets1"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = httpClient.execute(request);
result = EntityUtils.toString(response.getEntity());
Log.i("Result", result);
} catch (IOException e) {
e.printStackTrace();
}
我的做法是正确的还是错误的?
不,这不是实际时间。您正在尝试在主线程上获取执行时间,但您的 post 查询在后台的另一个线程中 运行。
这样使用
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> postParameters = new ArrayList<>();
postParameters.add(new BasicNameValuePair("test", "tets1"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
long start = System.nanoTime();
HttpResponse response = httpClient.execute(request);
long end = System.nanoTime() - start;
// Print value of end here
result = EntityUtils.toString(response.getEntity());
Log.i("Result", result);
} catch (IOException e) {
e.printStackTrace();
}
不对,这是错误的。在执行请求之前,AsyncTask
returns 上的 execute
方法立即生效。
如果将计时代码移到 AsyncTask
中,特别是将其放在这一行周围
HttpResponse response = httpClient.execute(request);
您将获得实际时间。
这是错误的,因为这些行没有按顺序执行。
new POSTTask().execute();
启动一个新线程,这意味着您的 2 个时间戳将非常接近,相隔几毫秒,这是不准确的
你应该在 doInBackground