use of com.eros.framework.extend.adapter.DefaultWXHttpAdapter in project WeexErosFramework by bmfe.
the class TypeFaceHandler method requestRemoteIcon.
private static void requestRemoteIcon(final WXRequest request, final IWXHttpAdapter.OnHttpListener listener) {
final WXResponse wxResponse = new WXResponse();
String method = request.method;
String requestBodyString = request.body == null ? "{}" : request.body;
RequestBody body = null;
if (request.paramMap != null && request.paramMap.containsKey("Content-Type")) {
body = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse(request.paramMap.get("Content-Type")), requestBodyString) : null;
} else {
body = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;" + "charset=UTF-8"), requestBodyString) : null;
}
Request.Builder requestBuilder = new Request.Builder().url(request.url).method(method, body);
if (request.paramMap != null) {
for (Map.Entry<String, String> param : request.paramMap.entrySet()) {
requestBuilder.addHeader(param.getKey(), TextUtil.toHumanReadableAscii(param.getValue()));
}
}
DefaultWXHttpAdapter adapter = (DefaultWXHttpAdapter) WXSDKManager.getInstance().getIWXHttpAdapter();
OkHttpClient httpClient = adapter.getHttpClient();
httpClient.newCall(requestBuilder.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
wxResponse.errorMsg = e.getMessage();
wxResponse.errorCode = "-1";
wxResponse.statusCode = "-1";
if (listener != null) {
listener.onHttpFinish(wxResponse);
}
}
@Override
public void onResponse(Call call, Response response) {
byte[] responseBody = new byte[0];
try {
responseBody = response.body().bytes();
} catch (IOException e) {
e.printStackTrace();
wxResponse.errorMsg = e.getMessage();
wxResponse.errorCode = "-1";
wxResponse.statusCode = "-1";
if (listener != null) {
listener.onHttpFinish(wxResponse);
}
}
wxResponse.data = new String(responseBody);
wxResponse.statusCode = String.valueOf(response.code());
wxResponse.originalData = responseBody;
wxResponse.extendParams = new HashMap<>();
for (Map.Entry<String, List<String>> entry : response.headers().toMultimap().entrySet()) {
wxResponse.extendParams.put(entry.getKey(), entry.getValue());
}
if (response.code() < 200 || response.code() > 299) {
wxResponse.errorMsg = response.message();
if (listener != null) {
listener.onHttpFinish(wxResponse);
}
} else {
// success
if (listener != null) {
listener.onHttpFinish(wxResponse);
}
}
}
});
}
Aggregations