使用OKHttp處理Volley的底層HTTP請求


1) 導入Volley(目前沒有官方的jar包,須要本身下載源碼編譯)
java

2) 導入OkHttp,okhttp-urlconnection ide

3) 建立OkHttpStack this

public class OkHttpStack extends HurlStack {
    private OkHttpClient okHttpClient;

    /**
     * Create a OkHttpStack with default OkHttpClient.
     */
    public OkHttpStack() {
        this(new OkHttpClient());
    }

    /**
     * Create a OkHttpStack with a custom OkHttpClient
     *
     * @param okHttpClient Custom OkHttpClient, NonNull
     */
    public OkHttpStack(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {
        OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient);
        return okUrlFactory.open(url);
    }
}

4) 建立Volley隊列 url

Volley.newRequestQueue(context, new OkHttpStack());
相關文章
相關標籤/搜索