@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: assert ((Map<String,String>)cb.mData.get("headers")).get("key").equals("someval"); }
@JSMethod(uiThread = false) public void fetch(String optionsStr, final JSCallback callback, JSCallback progressCallback){ JSONObject optionsObj = null; try { optionsObj = JSON.parseObject(optionsStr); }catch (JSONException e){ WXLogUtils.e("", e); } boolean invaildOption = optionsObj==null || optionsObj.getString("url")==null; if(invaildOption){ if(callback != null) { Map<String, Object> resp = new HashMap<>(); resp.put("ok", false); resp.put(STATUS_TEXT, Status.ERR_INVALID_REQUEST); callback.invoke(resp); } return; } String method = optionsObj.getString("method"); String url = optionsObj.getString("url"); JSONObject headers = optionsObj.getJSONObject("headers"); String body = optionsObj.getString("body"); String type = optionsObj.getString("type"); int timeout = optionsObj.getIntValue("timeout"); if (method != null) method = method.toUpperCase(); Options.Builder builder = new Options.Builder() .setMethod(!"GET".equals(method) &&!"POST".equals(method) &&!"PUT".equals(method) &&!"DELETE".equals(method) &&!"HEAD".equals(method) &&!"PATCH".equals(method)?"GET":method) .setUrl(url) .setBody(body) .setType(type) .setTimeout(timeout); extractHeaders(headers,builder); final Options options = builder.createOptions(); sendRequest(options, new ResponseCallback() { @Override public void onResponse(WXResponse response, Map<String, String> headers) { if(callback != null) { Map<String, Object> resp = new HashMap<>(); if(response == null|| "-1".equals(response.statusCode)){ resp.put(STATUS,-1); resp.put(STATUS_TEXT,Status.ERR_CONNECT_FAILED); }else { int code = Integer.parseInt(response.statusCode); resp.put(STATUS, code); resp.put("ok", (code >= 200 && code <= 299)); if (response.originalData == null) { resp.put("data", null); } else { String respData = readAsString(response.originalData, headers != null ? getHeader(headers, "Content-Type") : "" ); try { resp.put("data", parseData(respData, options.getType())); } catch (JSONException exception) { WXLogUtils.e("", exception); resp.put("ok", false); resp.put("data","{'err':'Data parse failed!'}"); } } resp.put(STATUS_TEXT, Status.getStatusText(response.statusCode)); } resp.put("headers", headers); callback.invoke(resp); } } }, progressCallback); }
WXStreamModule extends WXModule { @JSMethod(uiThread = false) public void fetch(String optionsStr, final JSCallback callback, JSCallback progressCallback){ JSONObject optionsObj = null; try { optionsObj = JSON.parseObject(optionsStr); }catch (JSONException e){ WXLogUtils.e("", e); } boolean invaildOption = optionsObj==null || optionsObj.getString("url")==null; if(invaildOption){ if(callback != null) { Map<String, Object> resp = new HashMap<>(); resp.put("ok", false); resp.put(STATUS_TEXT, Status.ERR_INVALID_REQUEST); callback.invoke(resp); } return; } String method = optionsObj.getString("method"); String url = optionsObj.getString("url"); JSONObject headers = optionsObj.getJSONObject("headers"); String body = optionsObj.getString("body"); String type = optionsObj.getString("type"); int timeout = optionsObj.getIntValue("timeout"); if (method != null) method = method.toUpperCase(); Options.Builder builder = new Options.Builder() .setMethod(!"GET".equals(method) &&!"POST".equals(method) &&!"PUT".equals(method) &&!"DELETE".equals(method) &&!"HEAD".equals(method) &&!"PATCH".equals(method)?"GET":method) .setUrl(url) .setBody(body) .setType(type) .setTimeout(timeout); extractHeaders(headers,builder); final Options options = builder.createOptions(); sendRequest(options, new ResponseCallback() { @Override public void onResponse(WXResponse response, Map<String, String> headers) { if(callback != null) { Map<String, Object> resp = new HashMap<>(); if(response == null|| "-1".equals(response.statusCode)){ resp.put(STATUS,-1); resp.put(STATUS_TEXT,Status.ERR_CONNECT_FAILED); }else { int code = Integer.parseInt(response.statusCode); resp.put(STATUS, code); resp.put("ok", (code >= 200 && code <= 299)); if (response.originalData == null) { resp.put("data", null); } else { String respData = readAsString(response.originalData, headers != null ? getHeader(headers, "Content-Type") : "" ); try { resp.put("data", parseData(respData, options.getType())); } catch (JSONException exception) { WXLogUtils.e("", exception); resp.put("ok", false); resp.put("data","{'err':'Data parse failed!'}"); } } resp.put(STATUS_TEXT, Status.getStatusText(response.statusCode)); } resp.put("headers", headers); callback.invoke(resp); } } }, progressCallback); } }
WXStreamModule extends WXModule { @JSMethod(uiThread = false) public void fetch(String optionsStr, final JSCallback callback, JSCallback progressCallback){ JSONObject optionsObj = null; try { optionsObj = JSON.parseObject(optionsStr); }catch (JSONException e){ WXLogUtils.e("", e); } boolean invaildOption = optionsObj==null || optionsObj.getString("url")==null; if(invaildOption){ if(callback != null) { Map<String, Object> resp = new HashMap<>(); resp.put("ok", false); resp.put(STATUS_TEXT, Status.ERR_INVALID_REQUEST); callback.invoke(resp); } return; } String method = optionsObj.getString("method"); String url = optionsObj.getString("url"); JSONObject headers = optionsObj.getJSONObject("headers"); String body = optionsObj.getString("body"); String type = optionsObj.getString("type"); int timeout = optionsObj.getIntValue("timeout"); if (method != null) method = method.toUpperCase(); Options.Builder builder = new Options.Builder() .setMethod(!"GET".equals(method) &&!"POST".equals(method) &&!"PUT".equals(method) &&!"DELETE".equals(method) &&!"HEAD".equals(method) &&!"PATCH".equals(method)?"GET":method) .setUrl(url) .setBody(body) .setType(type) .setTimeout(timeout); extractHeaders(headers,builder); final Options options = builder.createOptions(); sendRequest(options, new ResponseCallback() { @Override public void onResponse(WXResponse response, Map<String, String> headers) { if(callback != null) { Map<String, Object> resp = new HashMap<>(); if(response == null|| "-1".equals(response.statusCode)){ resp.put(STATUS,-1); resp.put(STATUS_TEXT,Status.ERR_CONNECT_FAILED); }else { int code = Integer.parseInt(response.statusCode); resp.put(STATUS, code); resp.put("ok", (code >= 200 && code <= 299)); if (response.originalData == null) { resp.put("data", null); } else { String respData = readAsString(response.originalData, headers != null ? getHeader(headers, "Content-Type") : "" ); try { resp.put("data", parseData(respData, options.getType())); } catch (JSONException exception) { WXLogUtils.e("", exception); resp.put("ok", false); resp.put("data","{'err':'Data parse failed!'}"); } } resp.put(STATUS_TEXT, Status.getStatusText(response.statusCode)); } resp.put("headers", headers); callback.invoke(resp); } } }, progressCallback); } WXStreamModule();  WXStreamModule(IWXHttpAdapter adapter); }
WXStreamModule extends WXModule { @JSMethod(uiThread = false) public void fetch(String optionsStr, final JSCallback callback, JSCallback progressCallback){ JSONObject optionsObj = null; try { optionsObj = JSON.parseObject(optionsStr); }catch (JSONException e){ WXLogUtils.e("", e); } boolean invaildOption = optionsObj==null || optionsObj.getString("url")==null; if(invaildOption){ if(callback != null) { Map<String, Object> resp = new HashMap<>(); resp.put("ok", false); resp.put(STATUS_TEXT, Status.ERR_INVALID_REQUEST); callback.invoke(resp); } return; } String method = optionsObj.getString("method"); String url = optionsObj.getString("url"); JSONObject headers = optionsObj.getJSONObject("headers"); String body = optionsObj.getString("body"); String type = optionsObj.getString("type"); int timeout = optionsObj.getIntValue("timeout"); if (method != null) method = method.toUpperCase(); Options.Builder builder = new Options.Builder() .setMethod(!"GET".equals(method) &&!"POST".equals(method) &&!"PUT".equals(method) &&!"DELETE".equals(method) &&!"HEAD".equals(method) &&!"PATCH".equals(method)?"GET":method) .setUrl(url) .setBody(body) .setType(type) .setTimeout(timeout); extractHeaders(headers,builder); final Options options = builder.createOptions(); sendRequest(options, new ResponseCallback() { @Override public void onResponse(WXResponse response, Map<String, String> headers) { if(callback != null) { Map<String, Object> resp = new HashMap<>(); if(response == null|| "-1".equals(response.statusCode)){ resp.put(STATUS,-1); resp.put(STATUS_TEXT,Status.ERR_CONNECT_FAILED); }else { int code = Integer.parseInt(response.statusCode); resp.put(STATUS, code); resp.put("ok", (code >= 200 && code <= 299)); if (response.originalData == null) { resp.put("data", null); } else { String respData = readAsString(response.originalData, headers != null ? getHeader(headers, "Content-Type") : "" ); try { resp.put("data", parseData(respData, options.getType())); } catch (JSONException exception) { WXLogUtils.e("", exception); resp.put("ok", false); resp.put("data","{'err':'Data parse failed!'}"); } } resp.put(STATUS_TEXT, Status.getStatusText(response.statusCode)); } resp.put("headers", headers); callback.invoke(resp); } } }, progressCallback); } WXStreamModule();  WXStreamModule(IWXHttpAdapter adapter); @Deprecated @JSMethod(uiThread = false) void sendHttp(String params, final String callback); @JSMethod(uiThread = false) void fetch(String optionsStr, final JSCallback callback, JSCallback progressCallback); }
WXStreamModule extends WXModule { @JSMethod(uiThread = false) public void fetch(String optionsStr, final JSCallback callback, JSCallback progressCallback){ JSONObject optionsObj = null; try { optionsObj = JSON.parseObject(optionsStr); }catch (JSONException e){ WXLogUtils.e("", e); } boolean invaildOption = optionsObj==null || optionsObj.getString("url")==null; if(invaildOption){ if(callback != null) { Map<String, Object> resp = new HashMap<>(); resp.put("ok", false); resp.put(STATUS_TEXT, Status.ERR_INVALID_REQUEST); callback.invoke(resp); } return; } String method = optionsObj.getString("method"); String url = optionsObj.getString("url"); JSONObject headers = optionsObj.getJSONObject("headers"); String body = optionsObj.getString("body"); String type = optionsObj.getString("type"); int timeout = optionsObj.getIntValue("timeout"); if (method != null) method = method.toUpperCase(); Options.Builder builder = new Options.Builder() .setMethod(!"GET".equals(method) &&!"POST".equals(method) &&!"PUT".equals(method) &&!"DELETE".equals(method) &&!"HEAD".equals(method) &&!"PATCH".equals(method)?"GET":method) .setUrl(url) .setBody(body) .setType(type) .setTimeout(timeout); extractHeaders(headers,builder); final Options options = builder.createOptions(); sendRequest(options, new ResponseCallback() { @Override public void onResponse(WXResponse response, Map<String, String> headers) { if(callback != null) { Map<String, Object> resp = new HashMap<>(); if(response == null|| "-1".equals(response.statusCode)){ resp.put(STATUS,-1); resp.put(STATUS_TEXT,Status.ERR_CONNECT_FAILED); }else { int code = Integer.parseInt(response.statusCode); resp.put(STATUS, code); resp.put("ok", (code >= 200 && code <= 299)); if (response.originalData == null) { resp.put("data", null); } else { String respData = readAsString(response.originalData, headers != null ? getHeader(headers, "Content-Type") : "" ); try { resp.put("data", parseData(respData, options.getType())); } catch (JSONException exception) { WXLogUtils.e("", exception); resp.put("ok", false); resp.put("data","{'err':'Data parse failed!'}"); } } resp.put(STATUS_TEXT, Status.getStatusText(response.statusCode)); } resp.put("headers", headers); callback.invoke(resp); } } }, progressCallback); } WXStreamModule();  WXStreamModule(IWXHttpAdapter adapter); @Deprecated @JSMethod(uiThread = false) void sendHttp(String params, final String callback); @JSMethod(uiThread = false) void fetch(String optionsStr, final JSCallback callback, JSCallback progressCallback); static final String STATUS_TEXT; static final String STATUS; }