weex icon indicating copy to clipboard operation
weex copied to clipboard

[Android] WXStreamModule 非 2xx 状态返回时,data为空

Open lanceliu opened this issue 10 years ago • 4 comments

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上

lanceliu avatar Nov 30 '16 02:11 lanceliu

欢迎提交PR。

sospartan avatar Dec 28 '16 02:12 sospartan

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.

YorkShen avatar Feb 18 '19 12:02 YorkShen

This issue is outdated for a long time and will be closed now. You can create a new one if you still have questions.

Hanks10100 avatar May 16 '21 10:05 Hanks10100

This issue is outdated for a long time and will be closed now. You can create a new one if you still have questions.

Hanks10100 avatar May 16 '21 10:05 Hanks10100