OkHttp请求request转换成字符串的顺序问题解决方案

JSONObject jsonObject;
if (requestBody.contentLength() == 0) {
    jsonObject = new JSONObject();
} else {
    jsonObject = new JSONObject(getParamContent(requestBody));
    Log.e(TAG, "request : " + getParamContent(requestBody));
}

String json = jsonObject.toString();
Log.e(TAG, "sign json : " + json);
signParams.put(REQUEST_PARAM, json);

//加签
sign = SignUtil.signRequest(signParams, SIGN_SECRET);


...

  /**
     * 获取常规post请求参数
     */
    private String getParamContent(RequestBody body) throws IOException {
        Buffer buffer = new Buffer();
        body.writeTo(buffer);
        return buffer.readUtf8();
    }

这种方式将request转换成json字符串会有斜杠问题,凡是右斜杠,后面总是多跟了一个左斜杠。

所以要直接用getParamContent获取字符串就行了。

getParamContent的源码可看出最终用的是sink.write()

@Override public void writeTo(BufferedSink sink) throws IOException {
        sink.write(content, offset, byteCount);
      }

这行代码的意思是:写byte数组,从offset开始,写byteCount长度。 
sink.write方法就类似于OutputStream的write方法,此时我们应该明白这行代码的含义了。

存不存在顺序问题呢?

猜你喜欢

转载自blog.csdn.net/T_yoo_csdn/article/details/82382935