導(dǎo)圖
簡(jiǎn)介
OkHttp是Square公司開源的一款面向Java和Android平臺(tái)的HTTP客戶端,是目前最主流的網(wǎng)絡(luò)框架之一。在Android4.4中,HttpUrlConnection底層已經(jīng)默認(rèn)使用okhttp實(shí)現(xiàn)。
特點(diǎn)
- 支持HTTP2/SPDY黑科技
- socket自動(dòng)選擇最好路線,并支持自動(dòng)重連
- 擁有自動(dòng)維護(hù)的socket連接池,減少握手次數(shù)
- 擁有隊(duì)列線程池,輕松寫并發(fā)
- 擁有Interceptors輕松處理請(qǐng)求與響應(yīng)(比如透明GZIP壓縮,LOGGING)
- 基于Headers的緩存策略
基本用法
創(chuàng)建OkHttpClient對(duì)象
OkHttpClient為OkHttp初始的配置中心,通過它可以完成初始參數(shù)的配置,設(shè)置超時(shí)時(shí)間,添加自定義攔截器,初始化線程池,代理等工作
public OkHttpClient() {
this(new Builder());
}
OkHttpClient(Builder builder) {
this.dispatcher = builder.dispatcher;
this.proxy = builder.proxy;
this.protocols = builder.protocols;
this.connectionSpecs = builder.connectionSpecs;
this.interceptors = Util.immutableList(builder.interceptors);
this.networkInterceptors = Util.immutableList(builder.networkInterceptors);
this.proxySelector = builder.proxySelector;
this.cookieJar = builder.cookieJar;
this.cache = builder.cache;
this.internalCache = builder.internalCache;
this.socketFactory = builder.socketFactory;
boolean isTLS = false;
for (ConnectionSpec spec : connectionSpecs) {
isTLS = isTLS || spec.isTls();
}
if (builder.sslSocketFactory != null || !isTLS) {
this.sslSocketFactory = builder.sslSocketFactory;
this.certificateChainCleaner = builder.certificateChainCleaner;
} else {
X509TrustManager trustManager = systemDefaultTrustManager();
this.sslSocketFactory = systemDefaultSslSocketFactory(trustManager);
this.certificateChainCleaner = CertificateChainCleaner.get(trustManager);
}
this.hostnameVerifier = builder.hostnameVerifier;
this.certificatePinner = builder.certificatePinner.withCertificateChainCleaner(
certificateChainCleaner);
this.proxyAuthenticator = builder.proxyAuthenticator;
this.authenticator = builder.authenticator;
this.connectionPool = builder.connectionPool;
this.dns = builder.dns;
this.followSslRedirects = builder.followSslRedirects;
this.followRedirects = builder.followRedirects;
this.retryOnConnectionFailure = builder.retryOnConnectionFailure;
this.connectTimeout = builder.connectTimeout;
this.readTimeout = builder.readTimeout;
this.writeTimeout = builder.writeTimeout;
this.pingInterval = builder.pingInterval;
}
兩種創(chuàng)建方式
- 默認(rèn)創(chuàng)建。直接new OkHttpClient,將使用Okhttp的默認(rèn)配置
- 自定義創(chuàng)建。通過指定一個(gè)Builder
創(chuàng)建Request對(duì)象
Request用于描述一個(gè)HTTP請(qǐng)求,比如請(qǐng)求的方法是"GET"還是"POST",請(qǐng)求的URL,請(qǐng)求的header,請(qǐng)求的body,請(qǐng)求的緩存策略等
//創(chuàng)建一個(gè)Request
Request request = new Request.Builder()
.url(url)
.build();
創(chuàng)建Call對(duì)象
Call是一次HTTP請(qǐng)求的Task,它會(huì)執(zhí)行網(wǎng)絡(luò)請(qǐng)求以獲得響應(yīng)。OkHttp中的網(wǎng)絡(luò)請(qǐng)求執(zhí)行Call既可以同步進(jìn)行,也可以異步進(jìn)行。調(diào)用call.execute()將直接執(zhí)行網(wǎng)絡(luò)請(qǐng)求,阻塞直到獲得響應(yīng)。而調(diào)用call.enqueue()傳入回調(diào),則會(huì)將Call放入一個(gè)異步執(zhí)行隊(duì)列,由ExecutorService在后臺(tái)執(zhí)行。
//new call
Call call = mOkHttpClient.newCall(request);
執(zhí)行網(wǎng)絡(luò)請(qǐng)求并獲取響應(yīng)
同步請(qǐng)求
同步請(qǐng)求會(huì)對(duì)當(dāng)前線程產(chǎn)生阻塞,直到請(qǐng)求響應(yīng)返回
Response response = client.newCall(request).execute();
異步請(qǐng)求
異步請(qǐng)求將提交到線程池中執(zhí)行
call.enqueue(new Callback(){
@Override
public void onResponse(final Response response) throws IOException{
//String htmlStr = response.body().string();
}
@Override
public void onFailure(Request request, IOException e) {
}
});
更多用法請(qǐng)參考 http://www.cnblogs.com/ct2011/p/3997368.html