目录
  • OkHttp中response.body().string()解析
    • 对于bytes()方法
    • 对于charset()方法
  • OkHttp的坑:response.body().string() 只能调用一次
    • 发现
    • 大致代码如下
  • 总结

    OkHttp中response.body().string()解析

    在多次引用response.body().string()的时候,程序会崩溃掉。

    下面通过源码分析:

    ResponseBody body = response.body();//获取响应体

    Response中的string()方法如下:

      public final String string() throws IOException {
      //通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String
        return new String(bytes(), charset().name());
      }

    对于bytes()方法

    public final byte[] bytes() throws IOException {
        long contentLength = contentLength();
        if (contentLength > Integer.MAX_VALUE) {
          throw new IOException("Cannot buffer entire body for content length: " + contentLength);
        }
        BufferedSource source = source();
        byte[] bytes;
        try {
          bytes = source.readByteArray();
        } finally {
          Util.closeQuietly(source);
        }
        if (contentLength != -1 && contentLength != bytes.length) {
          throw new IOException("Content-Length and stream length disagree");
        }
        return bytes;
      }

    可以看到,在finally中,执行了资源的关闭操作。

    在拿到资源之后,就将资源关闭了,所以只能获取一次实体。

    对于charset()方法

    private Charset charset() {
     MediaType contentType = contentType();
     return contentType != null ? contentType.charset(UTF_8) : UTF_8;
      }
    public static final Charset UTF_8 = Charset.forName("UTF-8");

    根据响应头中的contentType 决定编码形式。转换为UTF-8.

    OkHttp的坑:response.body().string() 只能调用一次

    发现

    在接微信登录时,通过构造 OkHttpClient 对象发起一次请求并加入队列,待服务端响应后,回调 Callback 接口触发 onResponse() 方法,然后在该方法中通过 Response 对象处理返回结果、实现业务逻辑。

    大致代码如下

      private void getUserInfo() {
            String path = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openid;
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(path)
                    .build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.d(TAG, "onFailure: userinfo" + e.getMessage());
                    finish();
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
    //                Log.d(TAG, "onResponse: userinfo" + response.body().string());    //okhttp中 response.body().string()只允许调用一次
                    final String result = response.body().string();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        unionId = jsonObject.getString("unionid");
                        headImgUrl = jsonObject.getString("headimgurl");
                        nickname = jsonObject.getString("nickname");
                        Log.d(TAG,"getUserInfo: unionId = "+unionId+"  headImgUrl = "+ headImgUrl + "  nickname = "+ nickname);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    finish();
                }
            });
        }

    在 onResponse() 中,为便于调试,我打印了返回体,然后通过 parseResponseStr() 方法解析返回体(注意:这儿两次调用了 response.body().string())。

    这段看起来没有任何问题的代码,实际运行后却出了问题,,通过debug发现result在转换成jsonObject时为null。

    那为什么result会变为null呢?通过网上资料查阅发现,response.body().string()只能调用一次,调用完就会释放掉资源,恍然大悟。。。

    然后我点进源码看了一下:

    public final String string() throws IOException {
        BufferedSource source = source();
        try {
          Charset charset = Util.bomAwareCharset(source, charset());
          return source.readString(charset);
        } finally {
          Util.closeQuietly(source);
        }
      }
    Util.closeQuietly(source);

    很棒,原来在我们调用了response.body的String()方法之后OkHttp 将响应体的缓冲资源返回的同时,调用 closeQuietly() 方法默默释放了资源。

    就是这个原因了。Get√

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。