30930

How to resend request with android volley when not authorized

In my android application I have to do some http request using android volley. If my request succeed everything's ok, the problem arise when I get an error and this error has <strong>status code 401</strong>. In this case I want to make some stuff and repeat the same request, same url and same parameters. Is there an official way to do that? If not, how can I get my params from error?

StringRequest req = new StringRequest(method, URL, new Response.Listener<String>() { @Override public void onResponse(String response){ //VolleyLog.v("Response:%n %s", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { NetworkResponse response = error.networkResponse; if(response.statusCode == 401){ //make some stuff... //here i want to resend my request } } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { //get headers } @Override public Map<String, String> getParams() throws AuthFailureError { //get params } }; // add the request object to the queue to be executed ApplicationController.getInstance().addToRequestQueue(req);

Any help would be appreciated.

Answer1:

You can create the RetryPolicy to change default retry behavior, only specify timeout milliseconds, retry count arguments :

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

the another way is estimate the VolleyError, re-execute the same request again when if was TimeoutError instance :

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

Hope this will help you

Answer2:

After modifying the request add the request in requestQueue. Do it in ErrorListener.

Recommend

  • Android: Dynamically Get JSON Array Key Name From JSON
  • Android JSON parsing arrays with GSON
  • Unable to send raw json data in post api data using Retrofit 2.0
  • android volley caching asynchronous requests - How te get url of the request in ResponseListener
  • Volley library NetworkDispatcher.run error
  • Removing event listeners on automatically created multiple elements
  • Java making confirming exit
  • Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance paramet
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Volley JsonObjectRequest send headers in GET Request
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to add date and time under each post in guestbook in google app engine
  • sending/ receiving email in Java
  • Is there a mandatory requirement to switch app.yaml?
  • Cannot Parse HTML Data Using Android / JSOUP
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory
  • JTable with a ScrollPane misbehaving
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Checking variable from a different class in C#
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?