4

I am having an issue with Volley and 301/302 redirects, specifically from http -> https. I am making API calls to a server that in the near future will be issuing a 301 redirect when calling http instead of https.

I am getting a ServerError on Response.ErrorListener.

I have read some solutions here, but most of them are 1+ years old.

What is the best option?

  1. Modify the Volley code to handle the 301/302 in the BasicNetwork class and Request class.

  2. Use an OkHttp3 stack instead of the default HttpStack of Volley. If so which one is recommended. I have seen multiple implementations.

Code Sample

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://www.server.com/api-path, null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {

                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {

                            }
                        });
VolleySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);

I am getting VolleyError of com.android.volley.ServerError with networkResponse.statusCode = 301. The server is returning the 301 with the location of https://www.server.com/api-path

j.t.h.
  • 121
  • 1
  • 11
  • Please provide a [mcve]. This would include the complete Java stack trace associated with the error, plus the Java code of yours referred to from that stack trace. – CommonsWare Feb 07 '17 at 19:31
  • updated with code sample – j.t.h. Feb 07 '17 at 20:25
  • From the volley library `} else { // 3xx? No reason to retry. throw new ServerError(networkResponse); }` – j.t.h. Feb 07 '17 at 20:30
  • If Volley does not handle a 301, IMHO that's a bug in Volley. That being said, rather than either of your options, I recommend simply getting rid of Volley and using OkHttp directly. There is no sense in wrapping OkHttp in Volley, as Volley will add no value. – CommonsWare Feb 07 '17 at 20:46
  • Even if I am just using OkHttp for the HttpStack as described [link](https://gist.github.com/alashow/c96c09320899e4caa06b) `public RequestQueue getRequestQueue() { if (mRequestQueue == null) { - mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); + mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext(), new OkHttp3Stack()); } return mRequestQueue; }` – j.t.h. Feb 08 '17 at 14:25
  • 1
    Looks like this may only be an issue 301/302 from http -> https. From Google Docs: HttpURLConnection will follow up to five HTTP redirects. It will follow redirects from one origin server to another. This implementation doesn't follow redirects from HTTPS to HTTP or vice versa. [link](https://developer.android.com/reference/java/net/HttpURLConnection.html) – j.t.h. Feb 28 '17 at 16:06

0 Answers0