我认为 HTTP 堆栈是要走的路。如果您根据 SDK 版本进行覆盖,则不会丢失自动 HttpClient 选择,就像 Volley 所做的那样。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
HurlStack stack = new HurlStack() {
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> headers)
throws IOException, AuthFailureError {
headers.putAll(MyApp.getAuthParams());
return super.performRequest(request, headers);
}
};
requestQueue = Volley.newRequestQueue(getApplicationContext(), stack);
} else {
HttpClientStack stack = new HttpClientStack(AndroidHttpClient.newInstance("volley/0")) {
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> headers)
throws IOException, AuthFailureError {
headers.putAll(MyApp.getAuthParams());
return super.performRequest(request, headers);
}
};
requestQueue = Volley.newRequestQueue(getApplicationContext(), stack);
}
参见Volley 源代码(第 53 行)。