Change Redirect Policy of Volley Framework

后端 未结 4 1949
别跟我提以往
别跟我提以往 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: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);
        }
    }
    

提交回复
热议问题