android – Picasso绑定适配器’连接被泄露’消息

前端之家收集整理的这篇文章主要介绍了android – Picasso绑定适配器’连接被泄露’消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用绑定适配器在回收器视图中加载图像.图像显得很好.快速滚动时,我注意到有时候我从毕加索那里得到了“连接泄露”的消息.

问题来自死图像链接,硬编码我的所有图像网址指向无处不在为屏幕上的第一对滚动后的每个图像产生错误.

  1. W/OkHttpClient: A connection to https://s3-eu-west-1.amazonaws.com/ was leaked. Did you forget to close a response body?

代码基本相同to this sample.

BindingUtils.kt

  1. object BindingUtils {
  2.  
  3. @BindingAdapter("imageUrl")
  4. @JvmStatic
  5. fun setImageUrl(imageView: ImageView,url: String) {
  6. Picasso.with(imageView.context).load(url).into(imageView)
  7. }

XML

  1. <ImageView
  2. android:id="@+id/imageview_merchant_background"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/primary"
  6. android:scaleType="centerCrop"
  7. app:imageUrl="@{viewmodel.background}"/>

gradle这个

  1. implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
  2. implementation "com.squareup.retrofit2:adapter-rxjava2:$rootProject.retrofitVersion"
  3. implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"
  4. implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.okhttpLoggingVersion"
  5. implementation "com.squareup.picasso:picasso:$rootProject.picassoVersion"
  6.  
  7. retrofitVersion = '2.3.0'
  8. okhttpLoggingVersion = '3.6.0'
  9. picassoVersion = '2.5.2'

我可以看到几个人需要关闭标准Okhttp请求的连接,但看到Picasso加载调用是一个单行的,这怎么可能泄漏?

解决方法

在引擎盖下,毕加索正在使用okhttp3来处理其网络请求.请参阅此处Picasso的NetworkRequestHandler类的代码https://github.com/square/picasso/blob/0728bb1c619746001c60296d975fbc6bd92a05d2/picasso/src/main/java/com/squareup/picasso/NetworkRequestHandler.java

有一个处理okhttp请求的加载函数

  1. @Override public Result load(Request request,int networkPolicy) throws IOException {
  2. okhttp3.Request downloaderRequest = createRequest(request,networkPolicy);
  3. Response response = downloader.load(downloaderRequest);
  4. ResponseBody body = response.body();
  5.  
  6. if (!response.isSuccessful()) {
  7. body.close();
  8. throw new ResponseException(response.code(),request.networkPolicy);
  9. }
  10.  
  11. // Cache response is only null when the response comes fully from the network. Both completely
  12. // cached and conditionally cached responses will have a non-null cache response.
  13. Picasso.LoadedFrom loadedFrom = response.cacheResponse() == null ? NETWORK : DISK;
  14.  
  15. // Sometimes response content length is zero when requests are being replayed. Haven't found
  16. // root cause to this but retrying the request seems safe to do so.
  17. if (loadedFrom == DISK && body.contentLength() == 0) {
  18. body.close();
  19. throw new ContentLengthException("Received response with 0 content-length header.");
  20. }
  21. if (loadedFrom == NETWORK && body.contentLength() > 0) {
  22. stats.dispatchDownloadFinished(body.contentLength());
  23. }
  24. InputStream is = body.byteStream();
  25. return new Result(is,loadedFrom);
  26. }

我对Picasso项目并不太熟悉,但似乎在所有情况下都没有关闭响应体对象.你可能已经发现了毕加索的一个错误,可能想在毕加索的github上提出一个问题

猜你在找的Android相关文章