android - picasso 套接字异常

标签 android picasso

当我尝试使用非常慢的连接 (GPRS) 从磁盘下载图像时,下载时间很长(大约 10 分钟),并且在从磁盘获取图像之前出现套接字异常。

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.4.0'

OkHttpClient client = new OkHttpClient();
    client.setCache(new Cache(context.getApplicationContext().getCacheDir(), Integer.MAX_VALUE));
    client.setConnectTimeout(5, TimeUnit.SECONDS); // connect timeout
    client.setReadTimeout(15, TimeUnit.SECONDS);    // socket timeout
    Picasso.Builder builder = new Picasso.Builder(this);
    builder.downloader(new OkHttpDownloader(client));
    Picasso built = builder.build();
    built.setIndicatorsEnabled(BuildConfig.DEBUG);
    built.setLoggingEnabled(BuildConfig.DEBUG);
    Picasso.setSingletonInstance(built);

enter image description here

enter image description here

提前致谢

PS:抱歉我的英语不好

最佳答案

我将自定义 Picasso 与我自己的 OKHTTP3 下载器一起使用,并将磁盘缓存超时设置为 6000 秒(100 分钟,哈哈)。根据需要修改 LRU->memory 和 Disk cache -> Cache

package com.example.project.recommendedapp;


import android.content.Context;
import android.util.Log;

import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.LruCache;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;

//Singleton Class for Picasso Downloading, Caching and Displaying Images Library
public class PicassoSingleton {

private static Picasso mInstance;
private static long mDiskCacheSize = 50*1024*1024; //Disk Cache limit 50mb

//private static int mMemoryCacheSize = 50*1024*1024; //Memory Cache 50mb, not currently using this. Using default implementation

private static OkHttpClient mOkHttp3Client; //OK Http Client for downloading
private static OkHttp3Downloader okHttp3Downloader;
private static Cache diskCache;
private static LruCache lruCache;//not using it currently


public static synchronized Picasso getSharedInstance(Context context)
{
    if(mInstance == null) {
        if (context != null) {
            //Create disk cache folder if does not exist
            File cache = new File(context.getApplicationContext().getCacheDir(), "picasso_cache");
            if (!cache.exists()) {
                cache.mkdirs();
            }

            diskCache = new Cache(cache, mDiskCacheSize);
            //lruCache = new LruCache(mMemoryCacheSize);//not going to be using it, using default memory cache currently
            lruCache = new LruCache(context); // This is the default lrucache for picasso-> calculates and sets memory cache by itself

            //Create OK Http Client with retry enabled, timeout and disk cache
            mOkHttp3Client = new OkHttpClient.Builder().cache(diskCache).connectTimeout(6000, TimeUnit.SECONDS).build();  //100 min cache timeout



            //For better performence in Memory use set memoryCache(Cache.NONE) in this builder (If needed)
            mInstance = new Picasso.Builder(context).memoryCache(lruCache).downloader(new OkHttp3Downloader(mOkHttp3Client)).indicatorsEnabled(true).build();

        }
    }
    return mInstance;
}

public static void deletePicassoInstance()
{
    mInstance = null;
}

public static void clearLRUCache()
{
    if(lruCache!=null) {
        lruCache.clear();
        Log.d("FragmentCreate","clearing LRU cache");
    }

    lruCache = null;

}

public static void clearDiskCache(){
    try {
        if(diskCache!=null) {
            diskCache.evictAll();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    diskCache = null;

}
}

关于android - picasso 套接字异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38396860/

相关文章:

java - RxJava2 - 合并流中的值和副作用

android - 如果前面的组被展开,则 Expanded ListView 的 setOnItemLongClickListener 给出不正确的 itemIndex

java - Android 3.0以下版本动画后按钮响应位置不变

android - BitmapHunter 异常 - 无法识别的请求类型

android - 如何在 Android Volley/Picasso/Glide 中更快地下载图像?

android - 从 Amazon S3 下载图像时如何使用 Picasso 库?

c# - Xamarin C# 安卓 : change color text in a substring?

android - 添加翻译数量多于应用程序的库会在 string.xml 中产生错误

android - 将 Picasso 与 RoundedBitmapDrawable 一起使用

java - ImageView 未根据 onActivityResult 中的更改进行更新