Find some examples of usage below (two first are taken from their documentation).
GET request:
GET request:
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
POST request:
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
This is my implementation of methods for HTTP calls.
private JSONObject request(String path, String method, String body) throws ApiException {
Builder builder = new Request.Builder()
.url(baseUrl + path.replaceAll("^/", ""));
if (method.equals("POST")) {
builder.post(RequestBody.create(JSON, body));
}
else if (method.equals("DELETE")) {
builder.delete();
}
try {
Response httpResponse = httpClient.newCall(builder.build()).execute();
if (httpResponse.code() != 200) {
throw new ApiException(httpResponse.body().string());
}
String httpResponseBody = httpResponse.body().string();
System.out.println("ApiClient.request(" + path + "): " + httpResponseBody);
return (JSONObject)JSONValue.parse(httpResponseBody);
}
catch (IOException e) {
throw new ApiException(e.getMessage());
}
}
public JSONObject get(String resource, Map<String, String> params) throws ApiException {
return request(resource + "?" + urlEncodeUTF8(params), "GET", "");
}
public JSONObject post(String path, String body) throws ApiException {
return request(path, "POST", body);
}
Hello there, my name is Leeroy and I can confirm that your handy HTTP client for Java works great. Thanks for this useful knowledge, and if you dont mind I would like to share with you this service where u can found articles about Java on any taste, for example recently I read one about how to parse json in java https://explainjava.com/parse-json-java/ and it was very interesting and useful for me.
ReplyDeleteI was very happy to find this site. I really enjoyed reading this article today and think it might be one of the best articles I have read so far. I wanted to thank you for this excellent reading !! I really enjoy every part and have bookmarked you to see the new things you post. Well done for this excellent article. Please keep this work of the same quality.
ReplyDeleteData Science Course in Bangalore
I want to say thanks to you. I have bookmark your site for future updates.
ReplyDeletedata science training
I am very blessed to find that code which i was searching for past 2 days basically i help students to find top essay writers Uk which is my part time job but really happy that my problem has been resolved from you provided code.
ReplyDeleteI have been searching for a handy HTTP client for Java, and I must say that it has greatly helped in integrating merchant cash advance service into my applications. It provides a seamless way to communicate with APIs and ensures smooth transaction processing. Highly recommended for developers in the financial services industry.
ReplyDeleteThe insights shared in this post are valuable for anyone starting their journey in data science.
ReplyDeleteKickstart your career by enrolling in this Data science course in Chennai