In Android, I'd like to use the new OkHttp 2.0 to request some URLs, but I'd like more control over redirects. I've already found the option to enable or disable following HTTPS → HTTP or HTTP → HTTPS redirects, but I'd like to not follow any redirects, so I can update my GUI as soon as possible, and choose whether to follow them from application logic. I don't see an option to do this. Is it possible, and if so, how can I achieve this?
Beginning with Mobile SDK 4.2, the Android REST request system uses OkHttp (v3. 2.0), an open-source external library from Square Open Source, as its underlying architecture. This library replaces the Google Volley library from past releases.
What is OkHttp? OkHttp is an HTTP client from Square for Java and Android applications. It's designed to load resources faster and save bandwidth. OkHttp is widely used in open-source projects and is the backbone of libraries like Retrofit, Picasso, and many others.
Yes it is possible in version 2.3.0
final OkHttpClient client = new OkHttpClient(); client.setFollowRedirects(false);
For 3.x
OkHttpClient client = new OkHttpClient().newBuilder() .followRedirects(false) .followSslRedirects(false) .build();
Seems like it isn't possible, but it will supposedly make it into the next release, at least according to some of these comments on a related issue on Github.
Edit: It looks like it could be possible through OkUrlFactory
, which is part of the okhttp-urlconnection submodule (still haven't figured out the actual purpose of this, but it looks like an Http(s)UrlConnection
replacement).
Edit 2: Actually, hang on; it looks like it is implemented on master. However, it also looks like this didn't make it into the 2.0.0 release snapshot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With