I am using OkHttpClient for the first time. It works very well, is very fast. So I understood that it uses an its own persistent cache, but when I request the folder where resides I receive null on getCache(). What is my misunderstanding?
OkHttpClient httpClient = new OkHttpClient();
Log.i("CACHE DIRECTORY", httpClient.getCache().getDirectory().toString());
Default impl of OkHttpClient has no cache. it provides a setCache method to customize one's own cache.
Related
For Android Apps, is it possible to set different caching times for different urls using OkHttpClient?
for example, I have two urls:
http://www.example.com/getcountries.php
http://www.example.com/getnews.php
for the first url, i would like to set caching for 365 days:
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder()
.maxStale(365, TimeUnit.DAYS)
.build())
.url("http://www.example.com/getcountries.php")
.build();
for the second url, i would like to set caching for 3 minutes:
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder()
.maxStale(3, TimeUnit.MINUTES)
.build())
.url("http://www.example.com/getnews.php")
.build();
will it work? (with caching in place, debugging is difficult).
Thanks for your support.
This will work but I think you want max-age and not max-stale. A cached response written at time a will be served until time b, a time that is derived from the response’s headers. The value you specify in max-stale is added to b to extend the lifetime of the cached response. The value you specify in max-age is added to a to constrain how long the cached response is valid.
https://square.github.io/okhttp/4.x/okhttp/okhttp3/-cache-control/-builder/
How to write picasso with Okhttp?
My code is given below but it is showing Error. By the way I am having confusion that picasso uses by default OkHttp or should I write it in code also?
OkHttpClient okHttpClient = new OkHttpClient();
RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(okHttpClient)).build();
OkHttpDownloader downloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(context).downloader(downloader).build();
Picasso.with(context).load("http://192.168.0.15:1337/offers/" + image_url.get(position)).resize(350, 100).centerCrop().into(holder.imageView);
No, you don't need to do things like that. OkHttp is a thing, which just make HTTP connection and loading files, for example, JSON easier. You should use it in separate thread, otherwise, you'll get an NetworkOnMainThreadException.
If you just need to download image, you won't need specific HTTP connection. Just use Picasso, for example, in onCreate() of the Activity, and enjoy the result.
All you need to do is:
Picasso.with(context)
.load("http://192.168.0.15:1337/offers/" + image_url.get(position))
.resize(350, 100)
.centerCrop()
.into(holder.imageView);
This may be a bug in Picasso, but I wanted to post to StackOverflow first.
I am getting the error "Received response with 0 content-length" when the responses are being read from the cache from disk. I can reproduce this error every time by
1) Run my app without OKHttp in classpath. Let pictures load
2) Add OkHttp into classpath, I get that error.
I added Picasso source to my project to investigate further. I found out that
1) Turning off caching connection.setUseCaches(false); will bypass the error (since it ignores the cache)
2) I found the switch in the Picasso source where it checks if OkHttp was available
try {
Class.forName("com.squareup.okhttp.OkHttpClient");
okHttpClient = true;
} catch (ClassNotFoundException ignored) {}
and was able to reproduce the bug by hardcoding true, then false between runs.
I want to solve this problem so I can use OKHttp (and provide a viable upgrade for my current users) and all the benefits that come with it. I also have seen this "reading response with no content-length from cache" problem in other cases in my live environment. Once I get into the state with a bad response in cache, the pictures will never show up.
OkHttpClient okHttpClient = new OkHttpClient();
RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(okHttpClient)).build();
OkHttpDownloader downloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(this).downloader(downloader).build();
Source: https://stackoverflow.com/a/23832172/2158970
I am using Picasso to load images for a listview.
The problem is internet connection is slow.
How can I change load timeout time in Picasso?
My code is :
Picasso.with(context)
.load(MainActivity.WEBSITE + book_item.Image)
.resize(final_thumb_width, final_thumb_height)
.into(new PicassoTarget(book_item,item.img, item.title));
You could possibly try something like this in your MainActivity's onCreate (or whereever you want to create the Picasso Builder
Picasso picasso;
OkHttpClient okHttpClient;
okHttpClient = new OkHttpClient();
okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
picasso = new Picasso.Builder(this)
.downloader(new OkHttpDownloader(okHttpClient))
.build();
That should give Picasso a timeout of ten seconds. Configure it to your needs.
Full Disclosure: I don't use a timeout. I just noticed this in the API. This may be completely wrong lol.
You have two options:
Subclass a Downloader class. Check this for reference implementation
Preconfigure OkHttpClient with timeouts and pass it to Picasso
I'm using picasso library to load images for my app. But I don't how to implement my own disk (sdcard) caching with picasso library.
Picasso uses the HTTP client for disk caching and if one is already configured it will use that instead of installing its own.
For the built-in UrlConnection the docs for installing a cache are here: https://developer.android.com/reference/android/net/http/HttpResponseCache.html
If you are using OkHttp then you just call setCache:
http://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/OkHttpClient.html#setCache-com.squareup.okhttp.Cache-
#Dax, to save files in custom cache directory using OkHttp, I would code something like this -
OkHttpClient okHttpClient = new OkHttpClient();
File customCacheDirectory = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/MyCache");
okHttpClient.setCache(new Cache(customCacheDirectory, Integer.MAX_VALUE));
OkHttpDownloader okHttpDownloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(mainActivity).downloader(okHttpDownloader).build();
picasso.load(imageURL).into(viewHolder.image);
Hope this helps.