com.taobao.weex.common.WXRequest Java Examples
The following examples show how to use
com.taobao.weex.common.WXRequest.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WXStreamModule.java From ucar-weex-core with Apache License 2.0 | 6 votes |
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."); } }
Example #2
Source File: WXStreamModuleTest.java From weex with Apache License 2.0 | 6 votes |
@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 = new WXStreamModule(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"); }
Example #3
Source File: WXStreamModuleTest.java From ucar-weex-core with Apache License 2.0 | 6 votes |
@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"); }
Example #4
Source File: DefaultUriAdapterTest.java From ucar-weex-core with Apache License 2.0 | 6 votes |
@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(); }
Example #5
Source File: WXStreamModule.java From weex-uikit with MIT License | 6 votes |
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."); } }
Example #6
Source File: DefaultUriAdapterTest.java From weex-uikit with MIT License | 6 votes |
@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(); }
Example #7
Source File: WXStreamModuleTest.java From weex-uikit with MIT License | 6 votes |
@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"); }
Example #8
Source File: OkHttpAdapter.java From Weex-OkHttp-Adapter with Apache License 2.0 | 5 votes |
private Headers AddHeaders(WXRequest request) { Headers.Builder builder = new Headers.Builder(); if (request.paramMap != null) { Set<String> keySets = request.paramMap.keySet(); for (String key : keySets) { builder.add(key, request.paramMap.get(key)); } } return builder.build(); }
Example #9
Source File: WXHttpAdapter.java From CrazyDaily with Apache License 2.0 | 5 votes |
private Headers getHeaders(WXRequest request) { Headers.Builder builder = new Headers.Builder(); if (request.paramMap != null) { Set<String> keySet = request.paramMap.keySet(); for (String key : keySet) { String value = request.paramMap.get(key); if (!TextUtils.isEmpty(value)) { builder.add(key, value); } } } return builder.build(); }
Example #10
Source File: WXStreamModuleTest.java From weex with Apache License 2.0 | 5 votes |
@Test public void testFetchStatus() throws Exception{ WXStreamModule streamModule = new WXStreamModule(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 = new WXStreamModule(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_TEXT),Status.getStatusText("302")); }
Example #11
Source File: DefaultWXHttpAdapter.java From weex with Apache License 2.0 | 5 votes |
/** * Opens an {@link HttpURLConnection} with parameters. * * @param request * @param listener * @return an open connection * @throws IOException */ private HttpURLConnection openConnection(WXRequest request, OnHttpListener listener) throws IOException { URL url = new URL(request.url); HttpURLConnection connection = createConnection(url); connection.setConnectTimeout(request.timeoutMs); connection.setReadTimeout(request.timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); if (request.paramMap != null) { Set<String> keySets = request.paramMap.keySet(); for (String key : keySets) { connection.addRequestProperty(key, request.paramMap.get(key)); } } if ("POST".equals(request.method)) { connection.setRequestMethod("POST"); if (request.body != null) { if (listener != null) { listener.onHttpUploadProgress(0); } connection.setDoOutput(true); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); //TODO big stream will cause OOM; Progress callback is meaningless out.write(request.body.getBytes()); out.close(); if (listener != null) { listener.onHttpUploadProgress(100); } } } else if (!TextUtils.isEmpty(request.method)) { connection.setRequestMethod(request.method); } else { connection.setRequestMethod("GET"); } return connection; }
Example #12
Source File: DefaultWXHttpAdapter.java From weex with Apache License 2.0 | 5 votes |
@Override public void sendRequest(final WXRequest request, final OnHttpListener listener) { if (listener != null) { listener.onHttpStart(); } execute(new Runnable() { @Override public void run() { WXResponse response = new WXResponse(); try { HttpURLConnection connection = openConnection(request, listener); Map<String,List<String>> headers = connection.getHeaderFields(); int responseCode = connection.getResponseCode(); if(listener != null){ listener.onHeadersReceived(responseCode,headers); } response.statusCode = String.valueOf(responseCode); if (responseCode >= 200 && responseCode<=299) { response.originalData = readInputStreamAsBytes(connection.getInputStream(), listener); } else { response.errorMsg = readInputStream(connection.getErrorStream(), listener); } if (listener != null) { listener.onHttpFinish(response); } } catch (IOException e) { e.printStackTrace(); response.statusCode = "-1"; response.errorCode="-1"; response.errorMsg=e.getMessage(); if(listener!=null){ listener.onHttpFinish(response); } } } }); }
Example #13
Source File: WXSDKInstance.java From weex-uikit with MIT License | 5 votes |
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())); }
Example #14
Source File: DefaultWXHttpAdapter.java From weex-uikit with MIT License | 5 votes |
/** * Opens an {@link HttpURLConnection} with parameters. * * @param request * @param listener * @return an open connection * @throws IOException */ private HttpURLConnection openConnection(WXRequest request, OnHttpListener listener) throws IOException { URL url = new URL(request.url); HttpURLConnection connection = createConnection(url); connection.setConnectTimeout(request.timeoutMs); connection.setReadTimeout(request.timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); if (request.paramMap != null) { Set<String> keySets = request.paramMap.keySet(); for (String key : keySets) { connection.addRequestProperty(key, request.paramMap.get(key)); } } if ("POST".equals(request.method) || "PUT".equals(request.method) || "PATCH".equals(request.method)) { connection.setRequestMethod(request.method); if (request.body != null) { if (listener != null) { listener.onHttpUploadProgress(0); } connection.setDoOutput(true); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); //TODO big stream will cause OOM; Progress callback is meaningless out.write(request.body.getBytes()); out.close(); if (listener != null) { listener.onHttpUploadProgress(100); } } } else if (!TextUtils.isEmpty(request.method)) { connection.setRequestMethod(request.method); } else { connection.setRequestMethod("GET"); } return connection; }
Example #15
Source File: WXSDKInstance.java From ucar-weex-core with Apache License 2.0 | 5 votes |
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; if(WXSDKManager.getInstance().getValidateProcessor()!=null) { mNeedValidate = WXSDKManager.getInstance().getValidateProcessor().needValidate(mBundleUrl); } 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.loadFileOrAsset(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())); WXHttpListener httpListener = new WXHttpListener(pageName, renderOptions, jsonInitData, flag, System.currentTimeMillis()); httpListener.setSDKInstance(this); adapter.sendRequest(wxRequest, (IWXHttpAdapter.OnHttpListener) httpListener); }
Example #16
Source File: DefaultWXHttpAdapter.java From ucar-weex-core with Apache License 2.0 | 5 votes |
/** * Opens an {@link HttpURLConnection} with parameters. * * @param request * @param listener * @return an open connection * @throws IOException */ private HttpURLConnection openConnection(WXRequest request, OnHttpListener listener) throws IOException { URL url = new URL(request.url); HttpURLConnection connection = createConnection(url); connection.setConnectTimeout(request.timeoutMs); connection.setReadTimeout(request.timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); if (request.paramMap != null) { Set<String> keySets = request.paramMap.keySet(); for (String key : keySets) { connection.addRequestProperty(key, request.paramMap.get(key)); } } if ("POST".equals(request.method) || "PUT".equals(request.method) || "PATCH".equals(request.method)) { connection.setRequestMethod(request.method); if (request.body != null) { if (listener != null) { listener.onHttpUploadProgress(0); } connection.setDoOutput(true); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); //TODO big stream will cause OOM; Progress callback is meaningless out.write(request.body.getBytes()); out.close(); if (listener != null) { listener.onHttpUploadProgress(100); } } } else if (!TextUtils.isEmpty(request.method)) { connection.setRequestMethod(request.method); } else { connection.setRequestMethod("GET"); } return connection; }
Example #17
Source File: DefaultWXHttpAdapter.java From weex-uikit with MIT License | 4 votes |
@Override public void sendRequest(final WXRequest request, final OnHttpListener listener) { if (listener != null) { listener.onHttpStart(); } execute(new Runnable() { @Override public void run() { WXResponse response = new WXResponse(); IEventReporterDelegate reporter = getEventReporterDelegate(); try { HttpURLConnection connection = openConnection(request, listener); reporter.preConnect(connection, request.body); Map<String,List<String>> headers = connection.getHeaderFields(); int responseCode = connection.getResponseCode(); if(listener != null){ listener.onHeadersReceived(responseCode,headers); } reporter.postConnect(); response.statusCode = String.valueOf(responseCode); if (responseCode >= 200 && responseCode<=299) { InputStream rawStream = connection.getInputStream(); rawStream = reporter.interpretResponseStream(rawStream); response.originalData = readInputStreamAsBytes(rawStream, listener); } else { response.errorMsg = readInputStream(connection.getErrorStream(), listener); } if (listener != null) { listener.onHttpFinish(response); } } catch (IOException|IllegalArgumentException e) { e.printStackTrace(); response.statusCode = "-1"; response.errorCode="-1"; response.errorMsg=e.getMessage(); if(listener!=null){ listener.onHttpFinish(response); } if (e instanceof IOException) { reporter.httpExchangeFailed((IOException) e); } } } }); }
Example #18
Source File: DefaultWXHttpAdapter.java From ucar-weex-core with Apache License 2.0 | 4 votes |
@Override public void sendRequest(final WXRequest request, final OnHttpListener listener) { if (listener != null) { listener.onHttpStart(); } execute(new Runnable() { @Override public void run() { WXResponse response = new WXResponse(); IEventReporterDelegate reporter = getEventReporterDelegate(); try { HttpURLConnection connection = openConnection(request, listener); reporter.preConnect(connection, request.body); Map<String,List<String>> headers = connection.getHeaderFields(); int responseCode = connection.getResponseCode(); if(listener != null){ listener.onHeadersReceived(responseCode,headers); } reporter.postConnect(); response.statusCode = String.valueOf(responseCode); if (responseCode >= 200 && responseCode<=299) { InputStream rawStream = connection.getInputStream(); rawStream = reporter.interpretResponseStream(rawStream); response.originalData = readInputStreamAsBytes(rawStream, listener); } else { response.errorMsg = readInputStream(connection.getErrorStream(), listener); } if (listener != null) { listener.onHttpFinish(response); } } catch (IOException|IllegalArgumentException e) { e.printStackTrace(); response.statusCode = "-1"; response.errorCode="-1"; response.errorMsg=e.getMessage(); if(listener!=null){ listener.onHttpFinish(response); } if (e instanceof IOException) { reporter.httpExchangeFailed((IOException) e); } } } }); }
Example #19
Source File: IWXHttpAdapter.java From weex with Apache License 2.0 | 2 votes |
/** * http request method * * @param request weex assemble request * @param listener http response notify */ void sendRequest(WXRequest request, OnHttpListener listener);
Example #20
Source File: IWXHttpAdapter.java From weex-uikit with MIT License | 2 votes |
/** * http request method * * @param request weex assemble request * @param listener http response notify */ void sendRequest(WXRequest request, OnHttpListener listener);
Example #21
Source File: IWXHttpAdapter.java From ucar-weex-core with Apache License 2.0 | 2 votes |
/** * http request method * * @param request weex assemble request * @param listener http response notify */ void sendRequest(WXRequest request, OnHttpListener listener);