Picasso绑定适配器'连接被泄露'消息

Dan*_*son 8 android android-image kotlin picasso android-databinding

我正在使用绑定适配器在回收器视图中加载图像.图像显得很好.快速滚动时,我注意到有时候我从毕加索那里得到了"连接泄露"的消息.

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

W/OkHttpClient: A connection to https://s3-eu-west-1.amazonaws.com/ was leaked. Did you forget to close a response body?
Run Code Online (Sandbox Code Playgroud)

代码与此示例基本相同.

BindingUtils.kt

object BindingUtils {

@BindingAdapter("imageUrl")
@JvmStatic
fun setImageUrl(imageView: ImageView, url: String) {
    Picasso.with(imageView.context).load(url).into(imageView)
}
Run Code Online (Sandbox Code Playgroud)

XML

<ImageView
android:id="@+id/imageview_merchant_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary"
android:scaleType="centerCrop"
app:imageUrl="@{viewModel.background}"/>
Run Code Online (Sandbox Code Playgroud)

gradle这个

implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
implementation "com.squareup.retrofit2:adapter-rxjava2:$rootProject.retrofitVersion"
implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"
implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.okhttpLoggingVersion"
implementation "com.squareup.picasso:picasso:$rootProject.picassoVersion"

retrofitVersion = '2.3.0'
okhttpLoggingVersion = '3.6.0'
picassoVersion = '2.5.2'
Run Code Online (Sandbox Code Playgroud)

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

don*_*uxx 2

在幕后,Picasso 使用 okhttp3 来处理其网络请求。请参阅此处 Picasso 的 NetworkRequestHandler 类的代码:https://github.com/square/picasso/blob/0728bb1c619746001c60296d975fbc6bd92a05d2/picasso/src/main/java/com/squareup/picasso/NetworkRequestHandler.java

有一个处理 okhttp 请求的 load 函数:

@Override public Result load(Request request, int networkPolicy) throws IOException {
    okhttp3.Request downloaderRequest = createRequest(request, networkPolicy);
    Response response = downloader.load(downloaderRequest);
    ResponseBody body = response.body();

    if (!response.isSuccessful()) {
      body.close();
      throw new ResponseException(response.code(), request.networkPolicy);
    }

    // Cache response is only null when the response comes fully from the network. Both completely
    // cached and conditionally cached responses will have a non-null cache response.
    Picasso.LoadedFrom loadedFrom = response.cacheResponse() == null ? NETWORK : DISK;

    // Sometimes response content length is zero when requests are being replayed. Haven't found
    // root cause to this but retrying the request seems safe to do so.
    if (loadedFrom == DISK && body.contentLength() == 0) {
      body.close();
      throw new ContentLengthException("Received response with 0 content-length header.");
    }
    if (loadedFrom == NETWORK && body.contentLength() > 0) {
      stats.dispatchDownloadFinished(body.contentLength());
    }
    InputStream is = body.byteStream();
    return new Result(is, loadedFrom);
  }
Run Code Online (Sandbox Code Playgroud)

我对 Picasso 项目不太熟悉,但似乎响应主体对象并非在所有情况下都关闭。您可能在 Picasso 中发现了一个错误,并且可能想在 picasso 的 github 上提交问题