RyuZz
RyuZz

Reputation: 581

Android volley JsonObjectRequest takes too long

I'm searching now for a few days to figure out why my POST request takes so long to load. I'm using the volley library instead of a HTTPRequest.

This is how I'm doing the request:

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);

The request takes about 10-15 seconds to load until I receive the response which is just ridiculous because the response size is about 1886 bytes. I tested it with a good WLAN connection and also with 3G.

If I'm doing the request with Postman on my Laptop over WLAN it only takes around 400ms so it's not a server-side problem. I'm confused why does it take so long to make this volley request, am I doing something wrong?

Upvotes: 0

Views: 3043

Answers (1)

ViramP
ViramP

Reputation: 1709

Normally volley not taken more time. you can check by your self.

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);
} 

If still you will face issue then you can go with Retrofit:http://square.github.io/retrofit/

Upvotes: 4

Related Questions