Search in sources :

Example 1 with DefaultWXHttpAdapter

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);
                }
            }
        }
    });
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) WXRequest(com.taobao.weex.common.WXRequest) Request(okhttp3.Request) IOException(java.io.IOException) WXResponse(com.taobao.weex.common.WXResponse) DefaultWXHttpAdapter(com.eros.framework.extend.adapter.DefaultWXHttpAdapter) Response(okhttp3.Response) WXResponse(com.taobao.weex.common.WXResponse) Callback(okhttp3.Callback) HashMap(java.util.HashMap) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Aggregations

DefaultWXHttpAdapter (com.eros.framework.extend.adapter.DefaultWXHttpAdapter)1 WXRequest (com.taobao.weex.common.WXRequest)1 WXResponse (com.taobao.weex.common.WXResponse)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1