Change Redirect Policy of Volley Framework

后端 未结 4 1923
别跟我提以往
别跟我提以往 2020-12-14 05:25

I am using the Volley framework in a project where I always need to handle the redirects myself to handle the headers.

How redirects are handled depends right now o

相关标签:
4条回答
  • 2020-12-14 05:36

    If you don't care about old APIs (< 9) and you just want volley stop following redirects you can do

    RequestQueue requestQueue = Volley.newRequestQueue(context, new HurlStack() {
        @Override
        protected HttpURLConnection createConnection(URL url) throws IOException {
            HttpURLConnection connection = super.createConnection(url);
            connection.setInstanceFollowRedirects(false);
    
            return connection;
        }
    });
    
    0 讨论(0)
  • 2020-12-14 05:45

    Try on more call with the Location value from the network response headers

    Kotlin Implementation

    fun fetchRemoteList(
        url: String,
        context: Context,
        callback: OnFetchListCompleted,
        firstTry: Boolean = true
    ) {
        val queue = Volley.newRequestQueue(context)
        val stringRequest = StringRequest(
            Request.Method.GET, url,
            Response.Listener { response ->
                GlobalScope.launch {
                    callback.fetchListSucceed()
                }
            },
            Response.ErrorListener { error ->
                val locationRedirectUrl: String? = error?.networkResponse?.headers?.get("Location")
    
                if (firstTry && !locationRedirectUrl.isNullOrEmpty()) {
                    fetchRemoteList(locationRedirectUrl, context, callback, false)
                } else {
                    callback.fetchListFailed(error?.message ?: "")
                }
            })
    
        queue.add(stringRequest)
    }
    
    0 讨论(0)
  • 2020-12-14 05:49

    This solution doesn't require another framework:

    Volley uses AndroidHTTPClient or Apache (if SDK level is 8 and under) by default. You could override / inherit the changes you want for redirection in these classes and create a custom HttpStack with them, feeding it to the Volley.newRequestQueue().

    EDIT:

    Assuming the very long named custom HttpStack implementations are yours:

    HttpStack stack;
    if (Build.VERSION.SDK_INT >= 9) {
        stack = new RedirectionHurlStack();
    } else {
        stack = new RedirectionHttpClientStack();
    }
    
    sRequestQueue = Volley.newRequestQueue(context, stack);
    
    0 讨论(0)
  • 2020-12-14 05:54

    I think A HttpStack implementation for Volley that uses OkHttp as its transport is the best solution

    RequestQueue queue = Volley.newRequestQueue(this);
    
    Network network = new BasicNetwork(new OkHttpStack());
    RequestQueue queue = new RequestQueue(new DiskBasedCache(new File(getCacheDir(), "volley")), network);
    queue.start();
    

    OkHttpStack class:

    public class OkHttpStack extends HurlStack {
    private final OkHttpClient client;
    
    public OkHttpStack() {
    this(new OkHttpClient());
    }
    
    public OkHttpStack(OkHttpClient client) {
    if (client == null) {
    throw new NullPointerException("Client must not be null.");
    }
    this.client = client;
    }
    
    @Override protected HttpURLConnection createConnection(URL url) throws IOException {
    return client.open(url);
    }
    }
    

    Update: if you are using new version of okhttp stack then use

    public class OkHttpStack extends HurlStack {
        private final OkUrlFactory mFactory;
    
        public OkHttpStack() {
            this(new OkHttpClient());
        }
    
        public OkHttpStack(OkHttpClient client) {
            if (client == null) {
                throw new NullPointerException("Client must not be null.");
            }
            mFactory = new OkUrlFactory(client);
        }
    
        @Override
        protected HttpURLConnection createConnection(URL url) throws IOException {
           return mFactory.open(url);
        }
    }
    
    0 讨论(0)
提交回复
热议问题