Skip to content

Instantly share code, notes, and snippets.

@anhulife
Created December 11, 2018 06:23
Show Gist options
  • Save anhulife/27c59f8679a01c1c0d5d2aa5aa13d851 to your computer and use it in GitHub Desktop.
Save anhulife/27c59f8679a01c1c0d5d2aa5aa13d851 to your computer and use it in GitHub Desktop.
Weex 的网络适配器,使用 OkHttp,并启用了缓存
import android.content.Context;
import android.text.TextUtils;
import com.taobao.weex.adapter.IWXHttpAdapter;
import com.taobao.weex.common.WXRequest;
import com.taobao.weex.common.WXResponse;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class WXHttpAdapter implements IWXHttpAdapter {
// 缓存大小为 10MB
private static final int CACHE_SIZE = 10 * 1024 * 1024;
// 网络请求客户端
private OkHttpClient mOkHttpClient;
public WXHttpAdapter(Context context) {
File cacheDir = new File(context.getCacheDir(), "weex_http_cache");
mOkHttpClient = new OkHttpClient.Builder()
.cache(new Cache(cacheDir, CACHE_SIZE))
.build();
}
@Override
public void sendRequest(final WXRequest request, final OnHttpListener listener) {
if (listener != null) {
listener.onHttpStart();
}
final WXResponse response = new WXResponse();
// 自定义超时时间的客户端
OkHttpClient okHttpClient = mOkHttpClient.newBuilder()
.connectTimeout(request.timeoutMs, TimeUnit.MILLISECONDS)
.readTimeout(request.timeoutMs, TimeUnit.MILLISECONDS)
.build();
// 请求头
Headers.Builder headersBuilder = new Headers.Builder();
if (request.paramMap != null) {
Set<String> keySets = request.paramMap.keySet();
for (String key : keySets) {
headersBuilder.add(key, request.paramMap.get(key));
}
}
Request.Builder okRequestBuilder = new Request.Builder()
.url(request.url)
.headers(headersBuilder.build());
if (!TextUtils.isEmpty(request.method)) {
RequestBody requestBody = null;
if (request.body != null) {
requestBody = RequestBody.create(null, request.body.getBytes());
}
okRequestBuilder.method(request.method, requestBody);
}
if (request.body != null && listener != null) {
listener.onHttpUploadProgress(0);
}
okHttpClient.newCall(okRequestBuilder.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
response.statusCode = "-1";
response.errorCode = "-1";
response.errorMsg = e.getMessage();
if (listener != null) {
listener.onHttpFinish(response);
}
}
@Override
public void onResponse(Call call, Response okResponse) throws IOException {
if (request.body != null && listener != null) {
listener.onHttpUploadProgress(100);
}
if (listener != null) {
listener.onHeadersReceived(okResponse.code(), okResponse.headers().toMultimap());
}
response.statusCode = String.valueOf(okResponse.code());
if (okResponse.isSuccessful()) {
response.originalData = okResponse.body().bytes();
} else {
response.errorMsg = okResponse.message();
}
if (listener != null) {
listener.onHttpFinish(response);
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment