T. Nowacki
T. Nowacki

Reputation: 109

Android Volley how catch timeout error in multiple send

I using volley to send gps positins, when Internet connection is back. So for example i have 100 records to send. Sometime internet connection is so slow or is phone loosing them. Timeout is set on 3 second so not all records are sending.

My question is, how error timeout catch from retryPolicy ? I want to know when must start sending again. Below my code:

stringRequest = new StringRequest(Request.Method.POST, syncDbDataDrive.SERVER_URL_POSITIONS,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String Response = jsonObject.getString("response");
                                        if (Response.equals("OK")) {
                                            syncDbHelper.updateLocalPositions(ID, Country, syncDbDataDrive.SYNC_STATUS_OK, database);
                                            rowAdd++;
                                            if (rowAdd == countRow) {
                                                database.close();
                                            }
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                        }
                    }) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> params = new HashMap<>();
                            params.put("latitude", Latitude);
                            params.put("longitude", Longitude);
                            params.put("country", Country);
                            return params;
                        }
                    };
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                            3000,  // timeout in miliseconds
                            0, // number of times retry
                            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

                 syncVolleySilgleton.getInstance(context).addToRequestQue(stringRequest);

Upvotes: 2

Views: 876

Answers (1)

TejaDroid
TejaDroid

Reputation: 6821

Try this, here you can check the error is of TimeoutError or not, if there time out error you can recall your task...

stringRequest.setRetryPolicy(new RetryPolicy() {
            @Override
            public int getCurrentTimeout() {
                return 50000;
            }

            @Override
            public int getCurrentRetryCount() {
                return 50000;
            }

            @Override
            public void retry(VolleyError error) throws VolleyError {
                 if(error instanceof TimeoutError){
                    // your stuf   
                }

            }
        });

Upvotes: 4

Related Questions