A Much Simpler HTTP Lib for Android
A much simpler HTTP lib for Android
A couple of days ago, I wrote up a post about Square’s OkHttp library. That was ok (pun intended), but kevinsawicki’s HttpRequest library is much simpler.
This is an excerpt from a demo app using HttpRequest, and it does the exact same thing as my OkHttp demo app:
String downloadStuff() { | |
String response = HttpRequest.get("http://example.com").body(); | |
// twiddle your thumbs, there's nothing left to do but return! | |
return response; | |
} |
Compare that with OkHttp’s version:
OkHttpClient client = new OkHttpClient(); | |
// Ignore invalid SSL endpoints. | |
client.setHostnameVerifier(new HostnameVerifier() { | |
@Override | |
public boolean verify(String s, SSLSession sslSession) { | |
return true; | |
} | |
}); | |
// Create request for remote resource. | |
HttpURLConnection connection; | |
connection = client.open(new URL(ENDPOINT)); | |
InputStream is = connection.getInputStream(); | |
InputStreamReader isr = new InputStreamReader(is); | |
// from StackOverflow: http://stackoverflow.com/a/2549222 | |
BufferedReader r = new BufferedReader(isr); | |
StringBuilder total = new StringBuilder(); | |
String line; | |
while ((line = r.readLine()) != null) { | |
total.append(line); | |
} | |
response = total.toString(); | |
BusProvider.getInstance().post(result); |
Notice the difference? HttpRequest only needs one line to do it’s thing, OkHttp needs a bit more. Now, I believe that OkHttp is intended to be a bit more general purpose, and it’s still pre-release, and without documentation. So, to be fair to OkHttp, I’m sure that there are things that it does really well, and I also think that it may be more performant than HttpRequest, at least based on the brief presentation that I heard on OkHttp.
Still, if I need to build something quickly, especially just for demonstration purposes, I really appreciate the brevity of HttpRequest.
Check out the full demo app on github. You can compare that with the demo that I did for OkHttp a few days ago. The only difference is the http library used, everything else is copied.