2016-08-19 189 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

相关问题