开发者

关于OkHttp中response.body().string()的用法解析

目录
  • 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.

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

    发现

    在接微信登录时,通过构造 OkHttpClient 对象发起一次请求并加入队列,待服务端响应后,回调 Callback 接口触发 onResponse() 方法,然后在该方法中通http://www.devze.com过 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 cawww.devze.comll, 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√

    总结

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

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜