[Android] WXStreamModule 非 2xx 状态返回时,data为空
WXStreamModule.fetch 发送请求返回处理是在DefaultWXHttpAdapter.sendRequest 处理的:
if (responseCode >= 200 && responseCode<=299) {
response.originalData = readInputStreamAsBytes(connection.getInputStream(), listener);
} else {
response.errorMsg = readInputStream(connection.getErrorStream(), listener);
}
所以只要是非2xx的状态码响应,stream的JS回调中 data中就没有数据返回。
建议在WXStreamModule.fetch中做返回结果做处理:
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 {
resp.put(STATUS, response.statusCode);
int code = Integer.parseInt(response.statusCode);
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"):""
);
resp.put("data",parseJson(respData,options.getType()));
}
resp.put(STATUS_TEXT, Status.getStatusText(response.statusCode));
}
resp.put("headers", headers);
callback.invoke(resp);
}
}
}, progressCallback);
response.originalData == null 时,不要直接把data value设定为null,把errorMsg值设定到data上
欢迎提交PR。
This PR/issue doesn't receive response since Weex migrated to Apache two years ago and this repository is no longer active yet.
Feel free to ask question in new repository and thanks for your contribution.
This issue is outdated for a long time and will be closed now. You can create a new one if you still have questions.
This issue is outdated for a long time and will be closed now. You can create a new one if you still have questions.