简体   繁体   中英

How to check when a request has been sent?

I'm sending a Volley StringRequest and it is taking upwards of three minutes to receive a response (but the response is status OK and the receiving action method works correctly).

Is there any way to check when the request has actually been SENT to the server (all the answers I've found about Volley requests talk about when the response has been received), so I can tell if it's just my phone taking 2 minutes to process the request before even transmitting or if the requestQueue is just taking a long time before it even gets to my request (though I only send one Volley request, on click, with priority set to IMMEDIATE).

Server-side logs show the prior request (a webview form submission) arriving a full 2-3 minutes prior to the StringRequest but I'm not sure if the delay is on cloudflare's side, the ELB or somewhere else and I want to know if I can discard the source device as being at fault.

Code follows for reference:

public void sendPostRequest(Map<String,String> params, String option)
{
    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
    String url="";
    switch (option){
        //...
        case "dniImages": //This is the one
            url  = BuildConfig.REST_DNI_IMAGES_ENDPOINT ;
            break;
    }

    StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new com.android.volley.Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            try{
            JSONObject resp = new JSONObject(response);}
            catch(JSONException e){}
        }
    }

    , new com.android.volley.Response.ErrorListener() { 
        @Override
        public void onErrorResponse(VolleyError error) {
            switch (option){
                //...
                case "dniImages":
                    Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }) {
        @Override
        public Priority getPriority() {
            return Priority.IMMEDIATE;
        }

        @Override
        protected com.android.volley.Response<String> parseNetworkResponse(NetworkResponse response) {
            if (response.statusCode == 200) {
                if (option.equals("dniImages"))
                    Toast.makeText(getApplicationContext(), "Error uploading images", Toast.LENGTH_SHORT).show();
            }
            return super.parseNetworkResponse(response);
        }
        protected Map<String, String> getParams() {
            return params;
        }
        @Override
        public Map<String, String> getHeaders(){
            try {
                Map<String, String> headers = super.getHeaders();
                if (headers == null
                        || headers.isEmpty()) {
                    headers = new HashMap<String, String>();
                }
                this.addSessionCookie(headers); 
                return headers;
            }
            catch (AuthFailureError e)
            {
            }
            return new HashMap<String, String>();
        }

        /**
         * Adds session cookie to headers if exists.
         * @param headers
         */
        public final void addSessionCookie(Map<String, String> headers) {
            String cookies = CookieManager.getInstance().getCookie(BuildConfig.TIENDAMIA_URL);
            headers.put("cookie", cookies);
        }

    };
    MyRequestQueue.add(MyStringRequest);
}

To be honest i haven't worked with Volley , but recently we had some few bugs related to networking and one of the tools that helped me a lot was the Android Profiler . I mean look at Network Profiler overview this is absolutely amazing.

Connection View: Lists files that were sent or received during the selected portion of the timeline across all of your app's CPU threads. For each request, you can inspect the size, type, status, and transmission duration. You can sort this list by clicking any of the column headers. You also see a detailed breakdown of the selected portion of the timeline, showing when each file was sent or received.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM