Search in sources :

Example 21 with WXRequest

use of com.taobao.weex.common.WXRequest in project incubator-weex by apache.

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(JSON.parseObject("{'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) WXSDKInstanceTest(com.taobao.weex.WXSDKInstanceTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with WXRequest

use of com.taobao.weex.common.WXRequest in project incubator-weex by apache.

the class WXStreamModule method sendRequest.

private void sendRequest(Options options, ResponseCallback callback, JSCallback progressCallback) {
    WXRequest wxRequest = new WXRequest();
    wxRequest.method = options.getMethod();
    wxRequest.url = mWXSDKInstance.rewriteUri(Uri.parse(options.getUrl()), URIAdapter.REQUEST).toString();
    wxRequest.body = options.getBody();
    wxRequest.timeoutMs = options.getTimeout();
    if (options.getHeaders() != null)
        if (wxRequest.paramMap == null) {
            wxRequest.paramMap = options.getHeaders();
        } else {
            wxRequest.paramMap.putAll(options.getHeaders());
        }
    IWXHttpAdapter adapter = (mAdapter == null && mWXSDKInstance != null) ? mWXSDKInstance.getWXHttpAdapter() : mAdapter;
    if (adapter != null) {
        adapter.sendRequest(wxRequest, new StreamHttpListener(callback, progressCallback));
    } else {
        WXLogUtils.e("WXStreamModule", "No HttpAdapter found,request failed.");
    }
}
Also used : IWXHttpAdapter(com.taobao.weex.adapter.IWXHttpAdapter) WXRequest(com.taobao.weex.common.WXRequest)

Example 23 with WXRequest

use of com.taobao.weex.common.WXRequest in project incubator-weex by apache.

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, true);
                } 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)

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