Search in sources :

Example 6 with WXRequest

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

the class DefaultUriAdapterTest method setup.

@Before
public void setup() {
    WXEnvironment.sApplication = RuntimeEnvironment.application;
    WXSDKManager wxsdkManager = WXSDKManager.getInstance();
    if (!new MockUtil().isSpy(wxsdkManager)) {
        WXSDKManager spy = Mockito.spy(wxsdkManager);
        WXSDKManagerTest.setInstance(spy);
        Mockito.when(spy.getIWXHttpAdapter()).thenReturn(new IWXHttpAdapter() {

            @Override
            public void sendRequest(WXRequest request, OnHttpListener listener) {
            //do nothing.
            }
        });
    }
    adapter = new DefaultUriAdapter();
    instance = WXSDKInstanceTest.createInstance();
}
Also used : MockUtil(org.mockito.internal.util.MockUtil) WXSDKManager(com.taobao.weex.WXSDKManager) WXRequest(com.taobao.weex.common.WXRequest) Before(org.junit.Before)

Example 7 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 8 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)

Example 9 with WXRequest

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

the class TypefaceUtil method downloadFontByNetwork.

private static void downloadFontByNetwork(final String url, final String fullPath, final String fontFamily) {
    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, new IWXHttpAdapter.OnHttpListener() {

        @Override
        public void onHttpStart() {
            if (WXEnvironment.isApkDebugable()) {
                WXLogUtils.d(TAG, "downloadFontByNetwork begin url:" + url);
            }
        }

        @Override
        public void onHeadersReceived(int statusCode, Map<String, List<String>> headers) {
        }

        @Override
        public void onHttpUploadProgress(int uploadProgress) {
        }

        @Override
        public void onHttpResponseProgress(int loadedLength) {
        }

        @Override
        public void onHttpFinish(WXResponse response) {
            int statusCode = 0;
            if (!TextUtils.isEmpty(response.statusCode)) {
                try {
                    statusCode = Integer.parseInt(response.statusCode);
                } catch (NumberFormatException e) {
                    statusCode = 0;
                    WXLogUtils.e(TAG, "IWXHttpAdapter onHttpFinish statusCode:" + response.statusCode);
                }
            }
            boolean result;
            if (statusCode >= 200 && statusCode <= 299 && response.originalData != null) {
                result = WXFileUtils.saveFile(fullPath, response.originalData, WXEnvironment.getApplication());
                if (result) {
                    result = loadLocalFontFile(fullPath, fontFamily);
                } else {
                    if (WXEnvironment.isApkDebugable()) {
                        WXLogUtils.d(TAG, "downloadFontByNetwork() onHttpFinish success, but save file failed.");
                    }
                }
            } else {
                result = false;
            }
            if (!result) {
                FontDO fontDO = sCacheMap.get(fontFamily);
                if (fontDO != null) {
                    fontDO.setState(FontDO.STATE_FAILED);
                }
            }
        }
    });
}
Also used : IWXHttpAdapter(com.taobao.weex.adapter.IWXHttpAdapter) List(java.util.List) WXRequest(com.taobao.weex.common.WXRequest) Paint(android.graphics.Paint) WXResponse(com.taobao.weex.common.WXResponse)

Example 10 with WXRequest

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

the class DefaultWXHttpAdapter method fetchUrl.

private void fetchUrl(final WXRequest request, final OnHttpListener 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()));
        }
    }
    client.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 {
                if (isInterceptor(request.url)) {
                    appendBaseJs(wxResponse, listener);
                    // listener);
                    if (listener != null) {
                        listener.onHttpFinish(wxResponse);
                    }
                } else {
                    // iconFont
                    if (listener != null) {
                        listener.onHttpFinish(wxResponse);
                    }
                }
            }
        }
    });
}
Also used : Call(okhttp3.Call) HashMap(java.util.HashMap) Request(okhttp3.Request) WXRequest(com.taobao.weex.common.WXRequest) IOException(java.io.IOException) WXResponse(com.taobao.weex.common.WXResponse) WXResponse(com.taobao.weex.common.WXResponse) Response(okhttp3.Response) Callback(okhttp3.Callback) Map(java.util.Map) HashMap(java.util.HashMap) RequestBody(okhttp3.RequestBody)

Aggregations

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