排球框架的变化重定向策略排球、重定向、框架、策略

2023-09-04 03:17:57 作者:%冷血豪情

我使用的一个项目,我总是需要处理重定向自己处理头凌空框架。

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

中如何处理重定向取决于现在的方法和传输层。我想用排枪(自动选择传输层)的默认值不改变任何凌空code。

How redirects are handled depends right now on the method and the transport layer. I would like to use the defaults of Volley (automatic selection of the transport layer) without changing any Volley code.

一个有效的解决方案是始终使用OkHttp作为传输层(如上文问题和贡献对于凌空),但我想知道是否有没有额外的框架的方法。

A valid solution is to always use OkHttp as a transport layer (as mentioned in Issues and contribution for Volley), but I would like to know if there is a way without an additional framework.

所以我要寻找一个干净的方式来禁用自动重定向处理。

Therefore I am looking for a "clean" way to disable automatic redirect handling.

编辑:

我preFER使用OkHttp,这样我就不必设法什么Android的自己,而是由板井Hanski提供的解决方案是很好用的是什么版本,想改变传输层的行为时。

I prefer to use OkHttp so that I don't have to manage what version to use on what Android myself, but the solution provided by Itai Hanski is very good to, when wanting to change the transport layer behavior.

推荐答案

我觉得 HttpStack 实施凌空使用OkHttp 的作为它的传输是最佳的解决方案。

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类:

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

更新:   如果您使用的是okhttp栈的新版本,然后使用

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