Android 如何测量服务器的请求和响应时间?
我正在使用asynctask和json解析从服务器获取响应,如何测量请求和响应时间,以下是我的web服务代码,有人能帮我吗Android 如何测量服务器的请求和响应时间?,android,json,httprequest,httpresponse,Android,Json,Httprequest,Httpresponse,我正在使用asynctask和json解析从服务器获取响应,如何测量请求和响应时间,以下是我的web服务代码,有人能帮我吗 public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } // function get json
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method.equals("POST")){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
long startTime = System.currentTimeMillis();
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("Total elapsed http request/response time in milliseconds: " + elapsedTime);
} else if(method.equals("GET")) {
//request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
long startTime = System.currentTimeMillis();
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("Total elapsed http request/response time in milliseconds: " + elapsedTime);
}
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (ClientProtocolException e) {
e.printStackTrace ();
}catch (IOException e) {
e.printStackTrace();
}
try {
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();
json = sb.toString();
Log.d("Request attempt","JSON >>>" + json.toString());
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result" + e.toString());
}
// try parse the string to a JSON object
try { jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON PArser", "Error Parsing data" + e.toString());
}
return jObj;
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
//函数从url获取json
//通过使用HTTP POST或GET方法
公共JSONObject makeHttpRequest(字符串url、字符串方法、,
列表参数){
//发出HTTP请求
试一试{
//检查请求方法
if(方法等于(“POST”)){
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
long startTime=System.currentTimeMillis();
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
long elapsedTime=System.currentTimeMillis()-startTime;
System.out.println(“http请求/响应的总运行时间(毫秒):+elapsedTime);
}else if(method.equals(“GET”)){
//请求方法是GET
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
long startTime=System.currentTimeMillis();
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
long elapsedTime=System.currentTimeMillis()-startTime;
System.out.println(“http请求/响应的总运行时间(毫秒):+elapsedTime);
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
Log.d(“请求尝试”、“JSON>>>”+JSON.toString());
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
try{jObj=newjsonobject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
返回jObj;
}
}请参阅下面的代码,您将了解如何执行此操作
long startTime = System.currentTimeMillis();
//Write this above before your httprequest
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
//After you get response
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("Total elapsed http request/response time in milliseconds: " + elapsedTime);
此代码将测量从您开始编写请求到您完成接收响应的时间,并显示结果。在get和post中..我可以粘贴相同的行吗?是的,我按照你说的粘贴..并运行我的应用..但在logcat中,没有任何显示像sysout一样..我接受并向上投票..谢谢它工作..它显示1688,这意味着什么?我没有得到你..什么?你能帮我吗?