'Cache for only specific URLs with retrofit

I need to cache response of only specific url addresses in my app; but what I found with many searches that I've done was cache with size limit for response; Anyone knows a way to do caching for only specific urls?



Solution 1:[1]

You can use CacheControl for this https://square.github.io/okhttp/4.x/okhttp/okhttp3/-cache/. Check the URL yourself and set it yourself before creating the call, or put the logic in an application interceptor.

Request request = new Request.Builder()
    .cacheControl(CacheControl.Builder()
      .noCache()
      .build())
    .url("http://publicobject.com/helloworld.txt")
    .build();

A tutorial https://developpaper.com/okhttp-cache-usage-guide/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yuri Schimke