Android volley to handle redirect

前端 未结 8 1231
名媛妹妹
名媛妹妹 2020-11-27 18:43

I recently started to use Volley lib from Google for my network requests. One of my requests get error 301 for redirect, so my question is that can volley handle redirect so

相关标签:
8条回答
  • 2020-11-27 19:18

    Replace your url like that url.replace("http", "https");

    for example: if your url looking like that : "http://graph.facebook......." than it should be like : "https://graph.facebook......."

    it works for me

    0 讨论(0)
  • 2020-11-27 19:21

    ok, im a bit late to the game here, but i've recently been trying to achieve this same aspect, so https://stackoverflow.com/a/17483037/2423312 is the best one, given that you are willing to fork volley and maintain it and the answer here : https://stackoverflow.com/a/27566737/2423312 - I'm not sure how this even worked.This one is spot on though : https://stackoverflow.com/a/28454312/2423312. But its actually adding a new request object to the NetworkDipatcher's queue, so you'll have to notify the caller as well somehow, there is one dirty way where you can do this by not modifying the request object + changing the field "mURL", PLEASE NOTE THAT THIS IS DEPENDENT ON YOUR IMPLEMENTATION OF VOLLEY'S RetryPolicy.java INTERFACE AND HOW YOUR CLASSES EXTENDING Request.java CLASS ARE, here you go : welcome REFLECTION

    Class volleyRequestClass = request.getClass().getSuperclass();
                            Field urlField = volleyRequestClass.getDeclaredField("mUrl");
                            urlField.setAccessible(true);
                            urlField.set(request, newRedirectURL);
    

    Personally I'd prefer cloning volley though. Plus looks like volley's example BasicNetwork class was designed to fail at redirects : https://github.com/google/volley/blob/ddfb86659df59e7293df9277da216d73c34aa800/src/test/java/com/android/volley/toolbox/BasicNetworkTest.java#L156 so i guess they arent leaning too much on redirects, feel free to suggest/edit. Always looking for good way..

    0 讨论(0)
  • 2020-11-27 19:24

    I am using volley:1.1.1 with https url though the request was having some issue. On digging deeper i found that my request method was getting changed from POST to GET due to redirect (permanent redirect 301). I am using using nginx and in server block i was having a rewrite rule that was causing the issue.

    So in short everything seems good with latest version of volley. My utility function here-

    public void makePostRequest(String url, JSONObject body, final AjaxCallback ajaxCallback) {
        try {
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                    url, body, new Response.Listener<JSONObject>() {
    
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(LOG, response.toString());
                    ajaxCallback.onSuccess(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(LOG, error.toString());
                    ajaxCallback.onError(error);
                }
            });
            singleton.getRequestQueue().add(jsonObjectRequest);
        } catch(Exception e) {
            Log.d(LOG, "Exception makePostRequest");
            e.printStackTrace();
        }
    }
    
    // separate file
    public interface AjaxCallback {
        void onSuccess(JSONObject response);
        void onError(VolleyError error);
    }
    
    0 讨论(0)
  • 2020-11-27 19:31

    Volley supports redirection without any patches, no need for a separate fork

    Explanation: Volley internally uses HttpClient which by default follows 301/302 unless specified otherwise

    From: http://hc.apache.org/httpcomponents-client-4.2.x/tutorial/html/httpagent.html

    ClientPNames.HANDLE_REDIRECTS='http.protocol.handle-redirects': defines whether redirects should be handled automatically. This parameter expects a value of type java.lang.Boolean. If this parameter is not set HttpClient will handle redirects automatically.

    0 讨论(0)
  • 2020-11-27 19:33

    If you dont want to modify the Volley lib you can catch the 301 and manually re-send the request.

    In your GsonRequest class implement deliverError and create a new Request object with the new Location url from the header and insert that to the request queue.

    Something like this:

    @Override
    public void deliverError(final VolleyError error) {
        Log.d(TAG, "deliverError");
    
        final int status = error.networkResponse.statusCode;
        // Handle 30x 
        if(HttpURLConnection.HTTP_MOVED_PERM == status || status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_SEE_OTHER) {
            final String location = error.networkResponse.headers.get("Location");
            Log.d(TAG, "Location: " + location);
            final GsonRequest<T> request = new GsonRequest<T>(method, location, jsonRequest, this.requestContentType, this.clazz, this.ttl, this.listener, this.errorListener);
            // Construct a request clone and change the url to redirect location.
            RequestManager.getRequestQueue().add(request);
        }
    }
    

    This way you can keep updating Volley and not have to worry about things breaking.

    0 讨论(0)
  • 2020-11-27 19:33

    Like many others, I was simply confused about why Volley wasn't following redirects automatically. By looking at the source code I found that while Volley will set the redirect URL correctly on its own, it won't actually follow it unless the request's retry policy specifies to "retry" at least once. Inexplicably, the default retry policy sets maxNumRetries to 0. So the fix is to set a retry policy with 1 retry (10s timeout and 1x back-off copied from default):

    request.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f))
    

    For reference, here is the source code:

    /**
     * Constructs a new retry policy.
     * @param initialTimeoutMs The initial timeout for the policy.
     * @param maxNumRetries The maximum number of retries.
     * @param backoffMultiplier Backoff multiplier for the policy.
     */
    public DefaultRetryPolicy(int initialTimeoutMs, int maxNumRetries, float backoffMultiplier) {
        mCurrentTimeoutMs = initialTimeoutMs;
        mMaxNumRetries = maxNumRetries;
        mBackoffMultiplier = backoffMultiplier;
    }
    

    Alternatively, you can create a custom implementation of RetryPolicy that only "retries" in the event of a 301 or 302.

    Hope this helps someone!

    0 讨论(0)
提交回复
热议问题