Android 重写OkHttpDownloader.load()后毕加索工作不正确
我对图像下载有以下要求:Android 重写OkHttpDownloader.load()后毕加索工作不正确,android,picasso,Android,Picasso,我对图像下载有以下要求: 忽略SSL错误(是的,我知道风险) 使用会话cookie 我尝试采用毕加索2.4.0来实现这一点,以下是我的方法: public static Picasso getPicasso(Context context) { /* an OkHttpClient that ignores SSL errors */ final OkHttpClient client = getUnsafeOkHttpClient(); return new
- 忽略SSL错误(是的,我知道风险)
- 使用会话cookie
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()
方法,因为它具有私有访问权限
请注意,给出的方法不起作用(响应代码401)
图像加载基本正常,但存在主要问题:
- 缓存不起作用(
总是fromCache
并且false
总是重新加载已下载的图像)Picasso
- 没有“内容长度”标题,因此
始终为-1contentLength
- 虽然缓存不起作用,但当加载下一个图像(完全相同或任何其他
)时,RAM的使用率会增加,因此ImageView
对象似乎停留在内存中的某个位置位图
- 在
的GridView
中使用时,似乎BaseAdapter
试图同时加载所有(或至少与调用Picasso
的次数相同)图像。这些图像出现后,应用程序冻结并关闭,并显示以下(OOM?)日志:getView()
A/活套﹕ 无法创建尾流管道。errno=24
或
无论我是否仅使用图像视图的自定义目标
,都会出现上述问题
似乎我通过覆盖OkHttpDownloader
的load()
方法打破了毕加索的一些机制,但我没有发现问题,因为我做了最小的更改。如果有人有类似的问题,我们将不胜感激。这真是我的错误。我创建了多个毕加索的实例,这完全是胡说八道。在使用返回单个Picasso
实例的helper类确保singleton模式后,一切都按预期工作
A/Looper﹕ Could not create epoll instance. errno=24