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 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.
ReplyDelete
ReplyDeleteThere is an error in the below code!
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);
This code is for a Lip Fillers Dubai website
"We will examine some of the well-known data science programmes offered in Pune that guarantee job placement for
ReplyDeletegraduates."
data science course in pune
embroidery digitizing services said...
ReplyDeleteOkHttp is undoubtedly a game-changer in the realm of Java-based HTTP clients. Its seamless integration, performance optimization, and user-friendly features make it the go-to choice for developers seeking efficient and reliable HTTP requests. Whether you're dealing with RESTful APIs or simply making network calls, OkHttp simplifies the process, offering a robust and elegant solution. Its flexibility and speed underscore its status as a standout tool in the developer's arsenal, enhancing the overall Java programming experience.
HTTP client for Java is essential for developers looking to create applications that interact with web services. Java offers several libraries like Http URL Connection Apache HttpClient and Says Professional CV Writing Dubai the modern Java HTTP Client API introduced in Java 11. These tools simplify sending HTTP requests and handling responses making it easier to integrate RESTful APIs into your applications.
ReplyDelete