java类com.squareup.picasso.Downloader的实例源码

OkHttp3DownloaderTest.java 文件源码 项目:picasso2-okhttp3-downloader 阅读 22 收藏 0 点赞 0 评论 0
@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();
}
ImageLoader.java 文件源码 项目:yApp 阅读 25 收藏 0 点赞 0 评论 0
/**
 * 构建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);
}
OkHttp3DownloaderTest.java 文件源码 项目:picasso2-okhttp3-downloader 阅读 24 收藏 0 点赞 0 评论 0
@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);
}
OkHttp3DownloaderTest.java 文件源码 项目:picasso2-okhttp3-downloader 阅读 27 收藏 0 点赞 0 评论 0
@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();
}
OkHttp3DownloaderTest.java 文件源码 项目:picasso2-okhttp3-downloader 阅读 24 收藏 0 点赞 0 评论 0
@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();
}
OkHttp3DownloaderTest.java 文件源码 项目:picasso2-okhttp3-downloader 阅读 23 收藏 0 点赞 0 评论 0
@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");
  }
}
ImageFragment.java 文件源码 项目:SteamGifts 阅读 24 收藏 0 点赞 0 评论 0
@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;
    }
}
ImageFragment.java 文件源码 项目:SteamGifts 阅读 27 收藏 0 点赞 0 评论 0
@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;
    }
}
Cache.java 文件源码 项目:photo-paper 阅读 23 收藏 0 点赞 0 评论 0
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);
    }
}
TracedPicassoDownloader.java 文件源码 项目:android-gluten 阅读 28 收藏 0 点赞 0 评论 0
private static Downloader createDefaultDownloader(Context context) {
  try {
    Class.forName("com.squareup.okhttp.OkHttpClient");
    return OkHttpLoaderCreator.create(context);
  } catch (ClassNotFoundException e) {
    return new UrlConnectionDownloader(context);
  }
}
ScreenSlidePageFragment.java 文件源码 项目:SadPanda 阅读 24 收藏 0 点赞 0 评论 0
@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);
}
OkHttp3DownloaderTest.java 文件源码 项目:picasso2-okhttp3-downloader 阅读 24 收藏 0 点赞 0 评论 0
@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);
}
ActivityModule.java 文件源码 项目:SongkickInterview 阅读 23 收藏 0 点赞 0 评论 0
public ActivityModule(Activity activity, Downloader okHttpDownloader) {
    this.activity = activity;
    this.okHttpDownloader = okHttpDownloader;
}
TracedPicassoDownloader.java 文件源码 项目:android-gluten 阅读 19 收藏 0 点赞 0 评论 0
public TracedPicassoDownloader(Tracer tracer, Downloader downloader) {
    this.tracer = tracer;
    this.downloader = downloader;
}
TracedPicassoDownloader.java 文件源码 项目:android-gluten 阅读 20 收藏 0 点赞 0 评论 0
static Downloader create(Context context) {
  return new OkHttpDownloader(context);
}
TracedPicassoDownloaderTest.java 文件源码 项目:android-gluten 阅读 19 收藏 0 点赞 0 评论 0
@Before
public void setup() {
    tracer = mock(Tracer.class);
    downloader = mock(Downloader.class);
    tested = new TracedPicassoDownloader(tracer, downloader);
}


问题


面经


文章

微信
公众号

扫码关注公众号