Search in sources :

Example 1 with IWXHttpAdapter

use of com.taobao.weex.adapter.IWXHttpAdapter in project weex-example by KalicyZhou.

the class WXDebugActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!WXEnvironment.isApkDebugable()) {
        finish();
        return;
    }
    setContentView(R.layout.activity_dynamic);
    mTipView = (TextView) findViewById(R.id.dynamic_tip);
    mResultView = (TextView) findViewById(R.id.dynamic_result);
    Uri uri = getIntent().getData();
    if (uri != null && uri.isHierarchical()) {
        String frameworkUrl = uri.getQueryParameter("framework");
        String bundleUrl = uri.getQueryParameter("bundle");
        if (!TextUtils.isEmpty(frameworkUrl)) {
            mTipView.setText("Dynamically replaces the framework!");
            IWXHttpAdapter adapter = WXSDKManager.getInstance().getIWXHttpAdapter();
            final WXRequest wxRequest = new WXRequest();
            wxRequest.url = frameworkUrl;
            adapter.sendRequest(wxRequest, this);
        } else if (!TextUtils.isEmpty(bundleUrl)) {
            mTipView.setText("Dynamically replaces the bundle!");
            WXEnvironment.sDynamicMode = uri.getBooleanQueryParameter("debug", false);
            WXEnvironment.sDynamicUrl = uri.getQueryParameter("bundle");
            String tip = WXEnvironment.sDynamicMode ? "Has switched to Dynamic Mode" : "Has switched to Normal Mode";
            Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}
Also used : IWXHttpAdapter(com.taobao.weex.adapter.IWXHttpAdapter) Uri(android.net.Uri) WXRequest(com.taobao.weex.common.WXRequest)

Example 2 with IWXHttpAdapter

use of com.taobao.weex.adapter.IWXHttpAdapter in project weex-example by KalicyZhou.

the class WXStreamModuleTest method testFetchStatus.

@Test
public void testFetchStatus() throws Exception {
    WXStreamModule streamModule = createModule(new IWXHttpAdapter() {

        @Override
        public void sendRequest(WXRequest request, OnHttpListener listener) {
            WXResponse response = new WXResponse();
            response.statusCode = "-1";
            listener.onHttpFinish(response);
        }
    });
    Callback finish = new Callback();
    streamModule.fetch("", finish, null);
    assertEquals(finish.mData.get(WXStreamModule.STATUS_TEXT), Status.ERR_INVALID_REQUEST);
    streamModule.fetch("{method: 'POST',url: 'http://httpbin.org/post',type:'json'}", finish, null);
    assertEquals(finish.mData.get(WXStreamModule.STATUS_TEXT), Status.ERR_CONNECT_FAILED);
    streamModule = createModule(new IWXHttpAdapter() {

        @Override
        public void sendRequest(WXRequest request, OnHttpListener listener) {
            WXResponse response = new WXResponse();
            response.statusCode = "302";
            listener.onHttpFinish(response);
        }
    });
    streamModule.fetch("{method: 'POST',url: 'http://httpbin.org/post',type:'json'}", finish, null);
    assertEquals(finish.mData.get(WXStreamModule.STATUS), 302);
    assertEquals(finish.mData.get(WXStreamModule.STATUS).getClass(), Integer.class);
    assertEquals(finish.mData.get(WXStreamModule.STATUS_TEXT), Status.getStatusText("302"));
}
Also used : JSCallback(com.taobao.weex.bridge.JSCallback) IWXHttpAdapter(com.taobao.weex.adapter.IWXHttpAdapter) WXRequest(com.taobao.weex.common.WXRequest) WXResponse(com.taobao.weex.common.WXResponse) Test(org.junit.Test) WXSDKInstanceTest(com.taobao.weex.WXSDKInstanceTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with IWXHttpAdapter

use of com.taobao.weex.adapter.IWXHttpAdapter in project weex-example by KalicyZhou.

the class WXStreamModuleTest method testFetchInvaildOptions.

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

        @Override
        public void sendRequest(WXRequest request, OnHttpListener listener) {
            listener.onHttpFinish(successResponse());
        }
    };
    WXStreamModule streamModule = new WXStreamModule(adapter);
    Callback cb = new Callback();
    streamModule.fetch("", 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 4 with IWXHttpAdapter

use of com.taobao.weex.adapter.IWXHttpAdapter in project weex-example by KalicyZhou.

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 5 with IWXHttpAdapter

use of com.taobao.weex.adapter.IWXHttpAdapter in project weex-example by KalicyZhou.

the class WXSDKInstance method renderByUrlInternal.

private void renderByUrlInternal(String pageName, final String url, Map<String, Object> options, final String jsonInitData, final WXRenderStrategy flag) {
    ensureRenderArchor();
    pageName = wrapPageName(pageName, url);
    mBundleUrl = url;
    Map<String, Object> renderOptions = options;
    if (renderOptions == null) {
        renderOptions = new HashMap<>();
    }
    if (!renderOptions.containsKey(BUNDLE_URL)) {
        renderOptions.put(BUNDLE_URL, url);
    }
    Uri uri = Uri.parse(url);
    if (uri != null && TextUtils.equals(uri.getScheme(), "file")) {
        render(pageName, WXFileUtils.loadAsset(assembleFilePath(uri), mContext), renderOptions, jsonInitData, flag);
        return;
    }
    IWXHttpAdapter adapter = WXSDKManager.getInstance().getIWXHttpAdapter();
    WXRequest wxRequest = new WXRequest();
    wxRequest.url = rewriteUri(Uri.parse(url), URIAdapter.BUNDLE).toString();
    if (wxRequest.paramMap == null) {
        wxRequest.paramMap = new HashMap<String, String>();
    }
    wxRequest.paramMap.put(KEY_USER_AGENT, WXHttpUtil.assembleUserAgent(mContext, WXEnvironment.getConfig()));
    adapter.sendRequest(wxRequest, new WXHttpListener(pageName, renderOptions, jsonInitData, flag, System.currentTimeMillis()));
}
Also used : IWXHttpAdapter(com.taobao.weex.adapter.IWXHttpAdapter) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) Uri(android.net.Uri) WXRequest(com.taobao.weex.common.WXRequest)

Aggregations

IWXHttpAdapter (com.taobao.weex.adapter.IWXHttpAdapter)8 WXRequest (com.taobao.weex.common.WXRequest)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 Uri (android.net.Uri)2 WXResponse (com.taobao.weex.common.WXResponse)2 Paint (android.graphics.Paint)1 JSONObject (com.alibaba.fastjson.JSONObject)1 WXDomObject (com.taobao.weex.dom.WXDomObject)1 List (java.util.List)1