Search in sources :

Example 11 with WXRequest

use of com.taobao.weex.common.WXRequest in project WeexErosFramework by bmfe.

the class AxiosManager method loadJSBundle.

public void loadJSBundle(final WXRequest request, final DefaultWXHttpAdapter.JSBundleCallback listener) {
    final WXResponse wxResponse = new WXResponse();
    String method = request.method == null ? "GET" : request.method.toUpperCase();
    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()));
        }
    }
    OkHttpUtils.getInstance().getOkHttpClient().newCall(requestBuilder.build()).enqueue(new okhttp3.Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            wxResponse.errorMsg = e.getMessage();
            wxResponse.errorCode = "-1";
            wxResponse.statusCode = "-1";
            if (listener != null) {
                listener.onFailure(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.onFailure(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.onFailure(wxResponse);
                }
            } else {
                if (listener != null) {
                    listener.onResponse(wxResponse);
                }
            }
        }
    });
}
Also used : Call(okhttp3.Call) HashMap(java.util.HashMap) OtherRequestBuilder(com.eros.framework.http.okhttp.builder.OtherRequestBuilder) GetBuilder(com.eros.framework.http.okhttp.builder.GetBuilder) PostFormBuilder(com.eros.framework.http.okhttp.builder.PostFormBuilder) OkHttpRequestBuilder(com.eros.framework.http.okhttp.builder.OkHttpRequestBuilder) WXRequest(com.taobao.weex.common.WXRequest) Request(okhttp3.Request) IOException(java.io.IOException) WXResponse(com.taobao.weex.common.WXResponse) Response(okhttp3.Response) WXResponse(com.taobao.weex.common.WXResponse) HashMap(java.util.HashMap) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Example 12 with WXRequest

use of com.taobao.weex.common.WXRequest in project WeexErosFramework by bmfe.

the class TypeFaceHandler method fetchIcon.

private static void fetchIcon(String url, IWXHttpAdapter.OnHttpListener listener) {
    IWXHttpAdapter adapter = WXSDKManager.getInstance().getIWXHttpAdapter();
    if (adapter == null) {
        WXLogUtils.e(TAG, "downloadFontByNetwork() IWXHttpAdapter == null");
        return;
    }
    WXRequest request = new WXRequest();
    request.url = url;
    request.method = "GET";
    adapter.sendRequest(request, listener);
}
Also used : IWXHttpAdapter(com.taobao.weex.adapter.IWXHttpAdapter) WXRequest(com.taobao.weex.common.WXRequest)

Example 13 with WXRequest

use of com.taobao.weex.common.WXRequest in project WeexErosFramework by bmfe.

the class TypeFaceHandler method load.

public static void load(String url, IWXHttpAdapter.OnHttpListener listener) {
    Uri parse = Uri.parse(url);
    listener.onHttpStart();
    DefaultTypefaceAdapter typefaceAdapter = BMWXApplication.getWXApplication().getTypefaceAdapter();
    if (typefaceAdapter == null) {
        Log.e(TAG, "未找到支持bmLocal的adapter");
        return;
    }
    if ("bmlocal".equalsIgnoreCase(parse.getScheme())) {
        // bmlocal
        if (typefaceAdapter.isInterceptor()) {
            // load local
            File iconDir = typefaceAdapter.getTypefaceDir();
            if (!iconDir.exists())
                return;
            File localIcon = new File(iconDir, parse.getPath());
            if (!localIcon.exists()) {
                // local icon not exists
                WXResponse response = new WXResponse();
                response.statusCode = "404";
                if (listener != null) {
                    listener.onHttpFinish(response);
                }
                return;
            }
            loadLocalIncon(localIcon, listener);
        } else {
            // fetch on Js server
            String iconDownloadUrl = typefaceAdapter.getJsServer() + "/dist";
            if (TextUtils.isEmpty(iconDownloadUrl))
                return;
            String fetchUrl = iconDownloadUrl + "/" + parse.getHost() + parse.getPath();
            fetchIcon(fetchUrl, listener);
        }
    } else if ("http".equalsIgnoreCase(parse.getScheme()) || "https".equalsIgnoreCase(parse.getScheme())) {
        // http
        WXRequest wxRequest = new WXRequest();
        wxRequest.url = url;
        wxRequest.method = "GET";
        requestRemoteIcon(wxRequest, listener);
    }
}
Also used : Uri(android.net.Uri) File(java.io.File) DefaultTypefaceAdapter(com.eros.framework.extend.adapter.DefaultTypefaceAdapter) WXRequest(com.taobao.weex.common.WXRequest) WXResponse(com.taobao.weex.common.WXResponse)

Example 14 with WXRequest

use of com.taobao.weex.common.WXRequest in project weex-example by KalicyZhou.

the class WXStreamModuleTest method testFetchSuccessFinish.

@Test
public void testFetchSuccessFinish() throws Exception {
    IWXHttpAdapter adapter = new IWXHttpAdapter() {

        @Override
        public void sendRequest(WXRequest request, OnHttpListener listener) {
            listener.onHttpFinish(successResponse());
        }
    };
    WXStreamModule streamModule = createModule(adapter);
    Callback cb = new Callback();
    streamModule.fetch("{'url':'http://www.taobao.com'}", cb, null);
    assert (boolean) cb.mData.get("ok");
}
Also used : JSCallback(com.taobao.weex.bridge.JSCallback) IWXHttpAdapter(com.taobao.weex.adapter.IWXHttpAdapter) WXRequest(com.taobao.weex.common.WXRequest) Test(org.junit.Test) WXSDKInstanceTest(com.taobao.weex.WXSDKInstanceTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with WXRequest

use of com.taobao.weex.common.WXRequest in project weex-example by KalicyZhou.

the class WXStreamModuleTest method testFetchHeaderReceived.

@Test
public void testFetchHeaderReceived() throws Exception {
    IWXHttpAdapter adapter = new IWXHttpAdapter() {

        @Override
        public void sendRequest(WXRequest request, OnHttpListener listener) {
            Map<String, List<String>> headers = new HashMap<>();
            headers.put("key", Arrays.asList("someval"));
            listener.onHeadersReceived(200, headers);
        }
    };
    WXStreamModule streamModule = createModule(adapter);
    Callback cb = new Callback();
    streamModule.fetch("{'url':'http://www.taobao.com'}", null, cb);
    assert ((Map<String, String>) cb.mData.get("headers")).get("key").equals("someval");
}
Also used : JSCallback(com.taobao.weex.bridge.JSCallback) IWXHttpAdapter(com.taobao.weex.adapter.IWXHttpAdapter) WXRequest(com.taobao.weex.common.WXRequest) Test(org.junit.Test) WXSDKInstanceTest(com.taobao.weex.WXSDKInstanceTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

WXRequest (com.taobao.weex.common.WXRequest)23 IWXHttpAdapter (com.taobao.weex.adapter.IWXHttpAdapter)17 WXResponse (com.taobao.weex.common.WXResponse)9 WXSDKInstanceTest (com.taobao.weex.WXSDKInstanceTest)8 JSCallback (com.taobao.weex.bridge.JSCallback)8 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 Uri (android.net.Uri)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Call (okhttp3.Call)3 Request (okhttp3.Request)3 RequestBody (okhttp3.RequestBody)3 Response (okhttp3.Response)3 Paint (android.graphics.Paint)2 JSONObject (com.alibaba.fastjson.JSONObject)2 WXSDKManager (com.taobao.weex.WXSDKManager)2 WXDomObject (com.taobao.weex.dom.WXDomObject)2