我想为我的 volley 网络调用添加鳍状肢支持,因为一些遗留调用仍然是用 volley 编写的。我试图实施此解决方案,但无法使其发挥作用。
类 OkHttpStack -
public class OkHttpStackApi extends HurlStack {
private final OkHttpClient client;
public OkHttpStackApi() {
this(new OkHttpClient());
}
public OkHttpStackApi(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 new OkUrlFactory(client).open(url);
}
}
CustomVolleyRequestQueue -
public class CustomVolleyRequestQueue {
private static CustomVolleyRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;
private CustomVolleyRequestQueue(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}
public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new CustomVolleyRequestQueue(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(mCtx, new OkHttpStackApi());
// Don't forget to start the volley request queue
mRequestQueue.start();
}
return mRequestQueue;
}
}
我无法弄清楚任何事情,因为我一直在使用 Retrofit 并且能够轻松地为其添加支持。