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.
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();
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.