OkHttp – android library that I can recommend for http requests from Your app.

Opublikowany

Recently for my private project I was forced to use HTTP GET request from my android application to HTTP server.
After some tries with HTTPUrlConnection which is OK but is hard to implement more complicated logic I’ve discovered project OkHttp (http://square.github.io/okhttp/).
It has all I needed out of the box.
Just added Gradle dependency in project build file.
compile «com.squareup.okhttp3:okhttp:3.5.0»

Simple HTTP GET looks like that:

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); return response.body().string();

Where:
client – is only one instance which is needed for multiple HTTP GET calls
request – represents current call
url – is the instance of URL class where in my case I had concatenated all parameters as ? … & with Base64 encoding
response – represent response data from the server

What surprised me is out of the box support for 302 response headers and easy cooperation with different servers.

Best regards
TJ

Autor