Java 类com.squareup.picasso.Downloader 实例源码

项目:picasso2-okhttp3-downloader    文件:OkHttp3DownloaderTest.java   
@Test public void networkPolicyNoCache() throws Exception {
  MockResponse response =
      new MockResponse().setHeader("Cache-Control", "max-age=31536000").setBody("Hi");
  server.enqueue(response);

  Downloader.Response response1 = downloader.load(uri, 0);
  assertThat((Boolean) cached.get(response1)).isFalse();
  // Exhaust input stream to ensure response is cached.
  Okio.buffer(Okio.source(response1.getInputStream())).readByteArray();

  // Enqueue the same response again but this time use NetworkPolicy.NO_CACHE.
  server.enqueue(response);

  Downloader.Response response2 = downloader.load(uri, NO_CACHE);
  // Response should not be coming from cache even if it was cached previously.
  assertThat((Boolean) cached.get(response2)).isFalse();
}
项目:yApp    文件:ImageLoader.java   
/**
 * 构建Picasso Downloader
 *
 * @param context
 * @param downloader
 */
public static void build(Context context, Downloader downloader) {
    Picasso picasso = new Picasso.Builder(context)
            .downloader(downloader)
            .build();
    Picasso.setSingletonInstance(picasso);
}
项目:picasso2-okhttp3-downloader    文件:OkHttp3DownloaderTest.java   
@Before public void setUp() throws Exception {
  downloader = new OkHttp3Downloader(temporaryFolder.getRoot());
  uri = Uri.parse(server.url("/").toString());

  cached = Downloader.Response.class.getDeclaredField("cached");
  cached.setAccessible(true);
}
项目:picasso2-okhttp3-downloader    文件:OkHttp3DownloaderTest.java   
@Test public void cachedResponse() throws Exception {
  server.enqueue(new MockResponse()
      .setHeader("Cache-Control", "max-age=31536000")
      .setBody("Hi"));

  Downloader.Response response1 = downloader.load(uri, 0);
  assertThat((Boolean) cached.get(response1)).isFalse();
  // Exhaust input stream to ensure response is cached.
  Okio.buffer(Okio.source(response1.getInputStream())).readByteArray();

  Downloader.Response response2 = downloader.load(uri, OFFLINE);
  assertThat((Boolean) cached.get(response2)).isTrue();
}
项目:picasso2-okhttp3-downloader    文件:OkHttp3DownloaderTest.java   
@Test public void offlineStaleResponse() throws Exception {
  server.enqueue(new MockResponse()
      .setHeader("Cache-Control", "max-age=1")
      .setHeader("Expires", "Mon, 29 Dec 2014 21:44:55 GMT")
      .setBody("Hi"));

  Downloader.Response response1 = downloader.load(uri, 0);
  assertThat((Boolean) cached.get(response1)).isFalse();
  // Exhaust input stream to ensure response is cached.
  Okio.buffer(Okio.source(response1.getInputStream())).readByteArray();

  Downloader.Response response2 = downloader.load(uri, OFFLINE);
  assertThat((Boolean) cached.get(response2)).isTrue();
}
项目:picasso2-okhttp3-downloader    文件:OkHttp3DownloaderTest.java   
@Test public void throwsResponseException() throws Exception {
  server.enqueue(new MockResponse().setStatus("HTTP/1.1 401 Not Authorized"));

  try {
    downloader.load(uri, 0);
    fail("Expected ResponseException.");
  } catch (Downloader.ResponseException e) {
    assertThat(e).hasMessage("401 Not Authorized");
  }
}
项目:SteamGifts    文件:ImageFragment.java   
@Override
protected byte[] doInBackground(Void... params) {
    try {
        // Grab an input stream to the image
        OkHttpDownloader downloader = new OkHttpDownloader(getContext());
        Downloader.Response response = downloader.load(Uri.parse(url), 0);

        // Read the image into a byte array
        return Okio.buffer(Okio.source(response.getInputStream())).readByteArray();
    } catch (Exception e) {
        Log.d(ImageFragment.class.getSimpleName(), "Error fetching image", e);
        return null;
    }
}
项目:SteamGifts    文件:ImageFragment.java   
@Override
protected byte[] doInBackground(Void... params) {
    try {
        // Grab an input stream to the image
        OkHttpDownloader downloader = new OkHttpDownloader(getContext());
        Downloader.Response response = downloader.load(Uri.parse(url), 0);

        // Read the image into a byte array
        return Okio.buffer(Okio.source(response.getInputStream())).readByteArray();
    } catch (Exception e) {
        Log.d(ImageFragment.class.getSimpleName(), "Error fetching image", e);
        return null;
    }
}
项目:photo-paper    文件:Cache.java   
public static Downloader createCacheDownloader(Context context) {
    try {
        Class.forName("com.squareup.okhttp.OkHttpClient");
        File cacheDir = createDefaultCacheDir(context, CACHE_PATH);
        long cacheSize = calculateDiskCacheSize(cacheDir);
        return new OkHttpDownloader(cacheDir, cacheSize);
    } catch (ClassNotFoundException e) {
        return new UrlConnectionDownloader(context);
    }
}
项目:android-gluten    文件:TracedPicassoDownloader.java   
private static Downloader createDefaultDownloader(Context context) {
  try {
    Class.forName("com.squareup.okhttp.OkHttpClient");
    return OkHttpLoaderCreator.create(context);
  } catch (ClassNotFoundException e) {
    return new UrlConnectionDownloader(context);
  }
}
项目:SadPanda    文件:ScreenSlidePageFragment.java   
@Override
public void onError() {
    if (SadPandaApp.getLastException() instanceof Downloader.ResponseException) {
        mFailureText.setText(R.string.image_not_found);
    }
    else {
        mFailureText.setText(R.string.image_loading_failed);
    }

    mFailureText.setVisibility(View.VISIBLE);

    mLoadingBar.setVisibility(View.GONE);
}
项目:picasso2-okhttp3-downloader    文件:OkHttp3DownloaderTest.java   
@Test public void readsContentLengthHeader() throws Exception {
  server.enqueue(new MockResponse().addHeader("Content-Length", 1024));

  Downloader.Response response = downloader.load(uri, 0);
  assertThat(response.getContentLength()).isEqualTo(1024);
}
项目:SongkickInterview    文件:ActivityModule.java   
public ActivityModule(Activity activity, Downloader okHttpDownloader) {
    this.activity = activity;
    this.okHttpDownloader = okHttpDownloader;
}
项目:android-gluten    文件:TracedPicassoDownloader.java   
public TracedPicassoDownloader(Tracer tracer, Downloader downloader) {
    this.tracer = tracer;
    this.downloader = downloader;
}
项目:android-gluten    文件:TracedPicassoDownloader.java   
static Downloader create(Context context) {
  return new OkHttpDownloader(context);
}
项目:android-gluten    文件:TracedPicassoDownloaderTest.java   
@Before
public void setup() {
    tracer = mock(Tracer.class);
    downloader = mock(Downloader.class);
    tested = new TracedPicassoDownloader(tracer, downloader);
}