毕加索在覆盖 OkHttpDownloader.load() 后工作不正常
Picasso working incorrectly after overriding OkHttpDownloader.load()
我对图片下载有以下要求:
忽略 SSL 错误(是的,我知道风险)
使用 session cookie
我尝试调整 Picasso 2.4.0 来做到这一点,下面是我的方法:
public static Picasso getPicasso(Context context) {
/* an OkHttpClient that ignores SSL errors */
final OkHttpClient client = getUnsafeOkHttpClient();
return new Picasso.Builder(context)
.downloader(new OkHttpDownloader(client) {
@Override
public Response load(Uri uri, boolean localCacheOnly) throws IOException {
final String RESPONSE_SOURCE_ANDROID = "X-Android-Response-Source";
final String RESPONSE_SOURCE_OKHTTP = "OkHttp-Response-Source";
HttpURLConnection connection = openConnection(uri);
connection.setRequestProperty("Cookie", getCookieHandler().
getCookieStore().getCookies().get(0).toString());
connection.setUseCaches(true);
if (localCacheOnly)
connection.setRequestProperty("Cache-Control", "only-if-cached,max-age=" + Integer.MAX_VALUE);
int responseCode = connection.getResponseCode();
if (responseCode == 401)
relogin();
else if (responseCode >= 300) {
connection.disconnect();
throw new ResponseException(responseCode + " " + connection.getResponseMessage());
}
String responseSource = connection.getHeaderField(RESPONSE_SOURCE_OKHTTP);
if (responseSource == null)
responseSource = connection.getHeaderField(RESPONSE_SOURCE_ANDROID);
long contentLength = connection.getHeaderFieldInt("Content-Length", -1);
boolean fromCache = parseResponseSourceHeader(responseSource);
return new Response(connection.getInputStream(), fromCache, contentLength);
}
}).build();
}
我对原始来源的唯一更改是为 HttpURLConnection
添加了 Cookie
。我还复制(未更改)parseResponseSourceHeader()
方法,因为它具有私有访问权限。
请注意,给定的方法 here 不起作用(响应代码 401)。
图片加载基本正常,但存在主要问题:
- 缓存不起作用(
fromCache
总是 false
并且 Picasso
总是重新加载已经下载的图像)
- 没有"Content-Length"header,所以
contentLength
总是-1
- 虽然缓存不起作用,但在加载下一张图像(完全相同或任何其他
ImageView
)时 RAM 使用量增加,似乎 Bitmap
object 保持不变记忆中的某处
- 当在
GridView
的 BaseAdapter
内部使用时,似乎 Picasso
尝试加载所有(或至少与 getView()
的次数一样多被称为)图像同时。出现这些图像,然后应用程序冻结并关闭并显示以下(OOM?)日志:
A/Looper﹕ Could not create wake pipe. errno=24
或
A/Looper﹕ Could not create epoll instance. errno=24
无论我是否仅使用 ImageView
.
的自定义 Target
,都会出现所描述的问题
我似乎通过重写 OkHttpDownloader
的 load()
方法破坏了一些 Picasso 机制,但我没有发现什么问题,因为我做了很少的改动。任何建议表示赞赏。
万一有人有类似的问题:那真是我的一个蹩脚错误。我正在创建多个 Picasso
实例,这完全是胡说八道。在使用助手 class 确保单例模式后 returns 单个 Picasso
实例一切正常。
我对图片下载有以下要求:
忽略 SSL 错误(是的,我知道风险)
使用 session cookie
我尝试调整 Picasso 2.4.0 来做到这一点,下面是我的方法:
public static Picasso getPicasso(Context context) {
/* an OkHttpClient that ignores SSL errors */
final OkHttpClient client = getUnsafeOkHttpClient();
return new Picasso.Builder(context)
.downloader(new OkHttpDownloader(client) {
@Override
public Response load(Uri uri, boolean localCacheOnly) throws IOException {
final String RESPONSE_SOURCE_ANDROID = "X-Android-Response-Source";
final String RESPONSE_SOURCE_OKHTTP = "OkHttp-Response-Source";
HttpURLConnection connection = openConnection(uri);
connection.setRequestProperty("Cookie", getCookieHandler().
getCookieStore().getCookies().get(0).toString());
connection.setUseCaches(true);
if (localCacheOnly)
connection.setRequestProperty("Cache-Control", "only-if-cached,max-age=" + Integer.MAX_VALUE);
int responseCode = connection.getResponseCode();
if (responseCode == 401)
relogin();
else if (responseCode >= 300) {
connection.disconnect();
throw new ResponseException(responseCode + " " + connection.getResponseMessage());
}
String responseSource = connection.getHeaderField(RESPONSE_SOURCE_OKHTTP);
if (responseSource == null)
responseSource = connection.getHeaderField(RESPONSE_SOURCE_ANDROID);
long contentLength = connection.getHeaderFieldInt("Content-Length", -1);
boolean fromCache = parseResponseSourceHeader(responseSource);
return new Response(connection.getInputStream(), fromCache, contentLength);
}
}).build();
}
我对原始来源的唯一更改是为 HttpURLConnection
添加了 Cookie
。我还复制(未更改)parseResponseSourceHeader()
方法,因为它具有私有访问权限。
请注意,给定的方法 here 不起作用(响应代码 401)。
图片加载基本正常,但存在主要问题:
- 缓存不起作用(
fromCache
总是false
并且Picasso
总是重新加载已经下载的图像) - 没有"Content-Length"header,所以
contentLength
总是-1 - 虽然缓存不起作用,但在加载下一张图像(完全相同或任何其他
ImageView
)时 RAM 使用量增加,似乎Bitmap
object 保持不变记忆中的某处 - 当在
GridView
的BaseAdapter
内部使用时,似乎Picasso
尝试加载所有(或至少与getView()
的次数一样多被称为)图像同时。出现这些图像,然后应用程序冻结并关闭并显示以下(OOM?)日志:
A/Looper﹕ Could not create wake pipe. errno=24
或
A/Looper﹕ Could not create epoll instance. errno=24
无论我是否仅使用 ImageView
.
Target
,都会出现所描述的问题
我似乎通过重写 OkHttpDownloader
的 load()
方法破坏了一些 Picasso 机制,但我没有发现什么问题,因为我做了很少的改动。任何建议表示赞赏。
万一有人有类似的问题:那真是我的一个蹩脚错误。我正在创建多个 Picasso
实例,这完全是胡说八道。在使用助手 class 确保单例模式后 returns 单个 Picasso
实例一切正常。