finding volley requests response time android
Asked Answered
C

1

8

I have been using volley for making rest calls to server. I am trying to send the response time for each requests to Google analytic to analyze the server latency, in volley jsonobject request is there any way to finding the response time for each requests. If volley doesn't provide this function how can I calculate the response time for each requests?.

Chaunce answered 1/4, 2015 at 8:35 Comment(0)
S
6

You'll need to calculate this yourself. Here's a quick way to see how long it takes for the response to arrive:

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);
}
Scandian answered 1/4, 2015 at 12:28 Comment(3)
Override parseNetworkResponse in request. Check NetworkResponse object. response.networkTimeMs should give you the valueHotchpot
With this method, you calculate the time between the moment when you add the request in the Volley queue and the response, not since the request is sending (if your Volley queue is not empty, your time can be false) The method posted by Shikhar Shrivastav is better.Tess
elapsedRealtime() should be used for interval timing.Parry

© 2022 - 2024 — McMap. All rights reserved.