I decided to write HTTP communication in Java, but which client library should I use? I was at a loss because there were many candidates.
What you have used, what you know
List the ones that appear in the search candidates of "ok http vs" and "apache http client vs" on Google
Some of them include not only HttpClient but also various functions such as JSON parsing. If you look for it, there seems to be more.
U, an early adopter colleague, used OkHttp in his recent app.
http://qiita.com/Reyurnible/items/33049c293c70bd9924ee → 2016-12-09 Retrofit recommendation
http://kikutaro777.hatenablog.com/entry/2015/07/19/044634 → 2015-07-19 I tried using Unirest
http://vividcode.hatenablog.com/entry/java/google-http-java-client → 2014-11-08 I tried using google-http-java-client
http://qiita.com/kou_hon/items/ddfb3fcb0277103e8b03 → Transfer from Volley to OkHttp
It's not a comparative article, so it's hard to judge, but each atmosphere is conveyed. According to 4, using Volley on Android seems to be tough.
http://stackoverflow.com/questions/1322335/what-is-the-best-java-library-to-use-for-http-post-get-etc → 2009 Commons HttpClient Recommended. It's just 7 years old, so it's old.
https://www.quora.com/What-is-the-best-both-fast-and-reliable-HTTP-client-library-in-Java → 2014-2015 Unirest, Jersey, Netty, OkHttp, etc. are recommended. It's too loose to be helpful.
The remaining libraries.
google-http-java-client vs OkHttp → Information cannot be found.
Unirest vs OkHttp → https://android.libhunt.com/project/unirest-java/vs/okhttp The name recognition is overwhelmingly OkHttp
OkHttp vs Jersey → I can't get information.
Due to the large number of articles, it seems that OkHttp has the most users. Jersey is a general word (jersey), so it's a mystery that's hard to google.
Compare code with common features. Sample to set header and body and POST.
OkHttp
OkHttpClient client = new OkHttpClient();
MediaType MIMEType= MediaType.parse("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create (MIMEType,"{}");
Request request = new Request.Builder().url(url).post(requestBody).build();
Response response = client.newCall(request).execute();
It's a little hard to read because I made too many objects.
google-http-java-client
Response response =
client
.preparePost("http://localhost:8080/post")
.setBody(builder.toString())
.execute()
.get();
It's refreshing in the method chain, but it seems to be misunderstood that it is get () at the end even though it is POST.
Unirest
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://localhost:8080/post")
.header("Content-Type", "application/json")
.queryString("apiKey", "123")
.field("param1", "nitori")
.field("param2", "1234")
.asJson();
This is also a method chain. There is nothing special to do.
Jersey
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/post").path("resource");
Form form = new Form();
form.param("x", "foo");
form.param("y", "bar");
MyJAXBBean bean =
target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
MyJAXBBean.class);
OkHttp, which has a large number of users and has established a de facto standard position, is the safest. If you close your eyes to being a minor, Unirest seems to be the most convenient and clean implementation in terms of code.
For more in-depth requirements, performance, cookie management, retries, multithreading, etc. need to be considered.
Java9 may or may not have HttpClient in the standard library. https://thoughtfulsoftware.wordpress.com/2016/09/12/java-9-the-new-httpclient/ But it's still not available on Android.
Recommended Posts