2016-08-19 185 views
0

我現在正在搜索幾天,找出爲什麼我的POST請求需要這麼長時間才能加載。我使用的是抽象庫而不是HTTPRequest。Android凌空JsonObjectRequest花費太長時間

這就是我正在做的請求:

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 

... 

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("geoLong", longitude); 
jsonObject.put("geoLat", latitude); 
jsonObject.put("radius", 5); 
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, URL, 
    jsonObject, createResponseListener(), createErrorListener()); 
jsonRequest.setShouldCache(false); 
jsonRequest.setRetryPolicy(new DefaultRetryPolicy(15000, 2, 1)); 
jsonRequest.setTag(requestTag); 
requestQueue.add(jsonRequest); 

請求大約需要10-15秒加載,直到我收到因爲響應的大小大約是1886年字節,這僅僅是荒謬的響應。我使用良好的WLAN連接和3G進行了測試。

如果我在我的筆記本電腦上通過無線局域網與Postman進行請求,它只需要大約400ms,所以它不是服務器端問題。我很困惑爲什麼需要這麼長時間才能完成這個抽象的請求,我做錯了什麼?

+0

你是如何測量時間?我在這裏什麼也看不到。 –

+0

我不測量手機上的時間,這只是我的評估。 – RyuZz

+1

那你怎麼知道這需要那麼久?你怎麼知道這個問題不在你的顯示代碼中?還是在其他十幾個地方?或者甚至有問題? –

回答

1

通常凌空抽不出更多時間。 你可以檢查你的自我。

private long mRequestStartTime; 

public void performRequest() 
{ 
    mRequestStartTime = System.currentTimeMillis(); // set the request start time just before you send the request. 

    JsonObjectRequest request = new JsonObjectRequest(URL, PARAMS, 
     new Response.Listener<JSONObject>() 
     { 
      @Override 
      public void onResponse(JSONObject response) 
      { 
       // calculate the duration in milliseconds 
       long totalRequestTime = System.currentTimeMillis() - mRequestStartTime; 
      } 
     }, 
     new Response.ErrorListener() 
     { 
      @Override 
      public void onErrorResponse(VolleyError error) 
      { 
       long totalRequestTime = System.currentTimeMillis() - mRequestStartTime; 
      } 
     }); 

    requestQueue.add(request); 
} 

如果你仍然會面臨問題,那麼你可以去改造:http://square.github.io/retrofit/

+0

我測量了你說的時間,問題是json解析。感謝您的幫助! – RyuZz

+0

做最好的編碼... – ViramP

相關問題