OnErrorResponse Android volley retry request

When I get an error message an android volley request. I want to repeat the request. How can I achieve this?

+4
source share
2 answers

Well, you can create RetryPolicyto alter the default behavior of repetition, specify only the arguments timeout milliseconds, retry count:

public class YourRequest extends StringRequest {
    public YourRequest(String url, Response.Listener<String> listener,
                       Response.ErrorListener errorListener) {
        super(url, listener, errorListener);
        setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    }
}

another way is to evaluate VolleyError, re-run the query if if was TimeoutErrorinstance:

public static void executeRequest() {
    RequestQueue.add(new YourRequest("http://your.url.com/", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (error instanceof TimeoutError) {
                // note : may cause recursive invoke if always timeout.
                executeRequest();
            }
        }
    }));
}

: " Volley ?", "". , Netroid, Volley , , , , , , :

final String REQUESTS_TAG = "Request-Demo";
String url = "http://facebook.com/";
JsonObjectRequest request = new JsonObjectRequest(url, null, new Listener<JSONObject>() {
    long startTimeMs;
    int retryCount;

    @Override
    public void onPreExecute() {
        startTimeMs = SystemClock.elapsedRealtime();
    }

    @Override
    public void onFinish() {
        RequestQueue.add(request);
        NetroidLog.e(REQUESTS_TAG);
    }

    @Override
    public void onRetry() {
        long executedTime = SystemClock.elapsedRealtime() - startTimeMs;
        if (++retryCount > 5 || executedTime > 30000) {
            NetroidLog.e("retryCount : " + retryCount + " executedTime : " + executedTime);
            mQueue.cancelAll(REQUESTS_TAG);
        } else {
            NetroidLog.e(REQUESTS_TAG);
        }
    }
});

request.setRetryPolicy(new DefaultRetryPolicy(5000, 20, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
request.setTag(REQUESTS_TAG);
RequestQueue.add(request);

Netroid , , :).

+7

,

  static int count=10; //so its will try ten time
public void userLogin(final View view)
{
    final RequestQueue requestQueue= Volley.newRequestQueue(getApplicationContext());
    String url = "http://192.168.43.107/mobodb/register.php";
  StringRequest stringRequest=new StringRequest(Request.Method.POST,url,new Response.Listener<String>()
    {
        @Override
        public void onResponse(String response) {
 Toast.makeText(getApplicationContext(),"Updated",Toast.LENGTH_LONG).show();
            }
        }
    },new Response.ErrorListener()
    {
        @Override
        public void onErrorResponse(VolleyError error) {
            count=count-1;
            Toast.makeText(getApplicationContext(),"Retry left"+count,Toast.LENGTH_LONG).show();
            if (count>0) {
                // note : may cause recursive invoke if always timeout.
                userLogin(view);
            }
            else
            {
                Toast.makeText(getApplicationContext(),"Request failed pls check network connection or the error is "+error.getMessage(),Toast.LENGTH_LONG).show();
            }
        }
    })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
           Map<String,String> paramter=new HashMap<String,String>();
        paramter.put("name",login_name);
            paramter.put("user_pass",login_pass);
            return paramter;
        }
    };
    requestQueue.add(stringRequest);
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 10, 1.0f));

, php java

@Override
        public void onResponse(String response) {

            if(response.contains("no record found for"))
            Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
            else
            {
                Toast.makeText(getApplicationContext(),"Updated num of row is"+response.toString(),Toast.LENGTH_LONG).show();
            }

        }

PHP-

if($res){
$resp=mysql_affected_rows();
if($resp==0)
{
$resp="no record found for".$_POST['name'];
}
if($resp==1 or $resp>1)
{
$resp=mysql_affected_rows();
}else $resp="efrror is".mysql_error();
0

Source: https://habr.com/ru/post/1547901/


All Articles