首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >毕加索SocketException

毕加索SocketException
EN

Stack Overflow用户
提问于 2016-07-15 13:03:42
回答 2查看 896关注 0票数 1

当我尝试从带有很慢连接(GPRS)的磁盘下载映像时,它很长(大约10分钟),在从磁盘获取映像之前,我会得到套接字异常。

代码语言:javascript
复制
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);

提前感谢

PS:对不起我的英语不好。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-15 13:57:29

我在自己的OKHTTP3下载程序中使用了自定义的Picasso,并将磁盘缓存超时设置为600(100分钟lol)。根据您的需要对LRU ->内存和磁盘缓存->缓存进行修补

代码语言:javascript
复制
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;

}
}
票数 2
EN

Stack Overflow用户

发布于 2016-07-15 13:36:49

可能您的服务器发送的文件没有适当的缓存头。在这种情况下,OKHTTP不会缓存图像。

毕加索没有磁盘缓存。它将委托给用于该功能的任何HTTP客户端(依赖HTTP缓存语义进行缓存控制)。

Using Picasso with custom disk cache

因此,下一次,它将重试下载。我见过从DropBox共享链接下载的任何图片都会发生这种情况。

缓存发生在http响应而不是映像中,因此如果响应没有服务器发送的适当头,则不会缓存它。看看是否是这样的。尝试使用另一个图像源进行测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38396860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档