gpt4 book ai didi

android - 我应该将 OkHttp 与 Volley 库一起使用吗?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:56 25 4
gpt4 key购买 nike

我在我的 Android 应用程序中使用 Volley 库。它工作正常,但我看到 OkHttp 也提供了更多改进。我已经使用以下方法将 OkHttp 与 Volley 集成:

Volley.newRequestQueue(mCtx.getApplicationContext(), new OkHttpStack());

我的 OkHttpStack 类是:

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

1) 值得吗?我没有注意到任何明显的改进,但这可能是因为我还没有在我的服务器上实现 SPDY 支持。

2) OkHttp 的增强之一是响应缓存。然而, Volley 也是如此。我会遇到类似这样的问题吗:https://github.com/square/okhttp/issues/680

3) 此外,我在 Volley 中使用了两个 RequestQueues - 一个用于图像,另一个用于 JSON。我应该对两个队列都使用 OkHttp 吗?

最佳答案

我建议您切换到不使用 okhttp-urlconnection 的堆栈,例如 -> https://goo.gl/ZZRSQ5

1) 是的,OkHttp 有很多优势,比如速度、HTTP/2、SPDY、节省带宽......

2) 我没有遇到任何问题。

3) 两者只需要一个 com.android.volley.RequestQueue。看看这个 -> https://goo.gl/GMn3Ls

我在这里写过关于 OkHttp + Volley + Gson 的文章 -> https://goo.gl/nl2DfN .我认为您可能会感兴趣。

关于android - 我应该将 OkHttp 与 Volley 库一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27960801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com