2016-07-15 206 views
0

當我試圖從連接速度很慢的連接(GPRS)的磁盤上下載鏡像時,鏡像很長(大約10分鐘),在從DISK獲取鏡像之前,我得到了Socket異常。Picasso SocketException

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:對不起我的英文不好

+0

從您要下載映像的服務器重置連接。畢加索必須下載一次圖像,然後它將緩存到磁盤緩存中。由於連接速度慢,第一次嘗試本身會被拒絕。 – Kushan

+0

嘗試增加連接超時值,說15分鐘 – Kushan

+0

@Kushan感謝您的答案,但我已經下載圖像的第一次。當我嘗試從磁盤獲取映像(已下載)時,此錯誤再次出現。 – Louis

回答

1

我用我自己的OKHTTP3下載器自定義畢加索,我設置磁盤緩存超時爲6000s(100mins lol)。根據需要使用LRU->內存和磁盤緩存 - > 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; 

} 
} 
+0

你可以設置setReadTimeout(xyz,TimeUnit.SECONDS);以及 – Kushan

+0

謝謝您的詳細解答 – Louis

0

這可能是你的服務器發送的文件,如果沒有適當的緩存頭。在這種情況下,您的OKHTTP不會緩存圖像。

Picasso沒有磁盤緩存。它委託給您用於該功能的任何HTTP客戶端(依靠HTTP緩存語義進行緩存控制)。

Using Picasso with custom disk cache

因此接下來的時間,它會重新嘗試下載。我從DropBox共享鏈接下載任何圖像時都看到過這種情況。

高速緩存發生在http響應而不是圖像上,所以如果響應沒有正確的頭由服務器發送,它將不會被高速緩存。檢查是否是這種情況。嘗試使用另一個圖像源進行測試。

+0

http://stackoverflow.com/questions/35806571/picasso-doesnt-cache-image-on-disk - >看到這裏的解釋,其相同的問題 – Kushan

+0

當我嘗試快速連接,它工作正常,調試指標是藍色(從磁盤)。 – Louis

+0

可能可能是httpresponse超時。正如我所說的緩存發生在ResponseCache或OkResponseCache上。如果響應緩存頭由於連接速度緩慢而超時,它將從磁盤緩存中被逐出 – Kushan