如何计算HTTP请求的执行时间

时间:2016-04-21 13:02:21

标签: java android json http

我有这样的函数,结果包含一个JSON对象。

public void rebuildhar() {

  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  StrictMode.setThreadPolicy(policy);

  List < MyResponse > list_response = new ArrayList < MyResponse > ();
  String[] parts = result.toString().split(",");

  List < String > list_parts = new ArrayList < String > ();
  String splitter = "\"url\":\"";

  for (int i = 0; i < parts.length; i++) {
   if (parts[i].startsWith(splitter)) {
    String clear_partial_string = parts[i].replace("\"url\":\"", "").replace("\"", "");
    list_parts.add(clear_partial_string);

    String address = clear_partial_string.replace("\\", "");

    try {
     url = new URL(address);
    } catch (MalformedURLException e) {
     Log.i(WEBPM_AGENT, e.toString());
    }

    if (url != null) {
     if (url.toString().contains("https")) {
      /*
      try {
      urlConnection = (HttpURLConnection) url.openConnection();
      } catch (IOException e) {
      e.printStackTrace();
      }
      urlConnection.setDoInput(true);
      urlConnection.setDoOutput(true);
      */
      wv = new WebView(AgentActivity.this);

      wv.getSettings().setLoadWithOverviewMode(true);
      wv.getSettings().setUseWideViewPort(true);
      wv.setInitialScale(1);
      wv.getSettings().setJavaScriptEnabled(true);

      wv.loadUrl(url.toString());
      wv.setWebViewClient(new WebViewClient() {


       @Override
       public void onPageStarted(WebView view, String url, Bitmap favicon) {
        tStart = System.currentTimeMillis();
       }

       @Override
       public void onPageFinished(WebView view, String url) {
        tEnd = System.currentTimeMillis();

        tDelta = (tEnd - tStart);

        System.out.println("DEBUG:" + "URL: " + url + "\n Time: " + tDelta/* +"\n Size: " +urlConnection.getContentLength()*/);

        tStart = 0;
        tEnd = 0;
        tDelta = 0;
       }
      });
      // list_response.add(new MyResponse(address,elapsedSeconds,urlConnection.getContentLength()));
     }
    }
   }
  }
 }

我获得的是:

  • 对于JSON数组中的每个URL(HTTPS),我想测试请求的执行时间。

    • 将此数据保存在arrayList

问题是tstart在首次运行后始终为0

但是这个解决方案并不起作用......

1 个答案:

答案 0 :(得分:0)

我们可以使用开发人员工具来计算响应时间。
通常在所有浏览器中按F12我们都可以得到它。

For Firfox help