Java OkHttps Library(Android/JAVA EE WEB)Using API Request Handle

1: Firstly you can Create Java Project

2:You can add Dependancies(search to find latest version) #https://square.github.io/okhttp/

3:Look This Code and create new java.file and Copy page

You can change HttpUrl variable valus and media type
RETURN JSON DATA SET AS A STRING
RETURN JSON DATA SET AS A STRING
Gson using Deserialize the Responce dataset and Its put to class array object
Your class variable names Match as a your Response Json Object data set values

LIKE THIS

content type should be json
  • GSon Librray using we can Deserialization Json object to class array object

https://www.tutorialspoint.com/gson/gson_quick_guide.htm

  • Additionaly You can use simple-json Library to read json responce data.

https://www.tutorialspoint.com/json_simple/json_simple_quick_guide.htm

package ConnectRestServer;

import java.io.IOException;

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;

import modelsPkg.CentersModel;

import okhttp3.Call;

import okhttp3.Callback;

import okhttp3.MediaType;

import okhttp3.OkHttpClient;

import okhttp3.Request;

import okhttp3.RequestBody;

import okhttp3.Response;

public class ConnectinServer {

public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse(“text/x-markdown; charset=utf-8”);

private final OkHttpClient client = new OkHttpClient();

private String httpUrl = “http://localhost:8080/HospitalRestServer/webapi/database";

public void GETDataFrommmmm(String endPointName) {

Request request = new Request.Builder().url(httpUrl + endPointName).build();

Call call = client.newCall(request);

call.enqueue(new Callback() {

public void onResponse(Call call, Response response) throws IOException {

System.out.print(response.body().string());

}

public void onFailure(Call call, IOException e) {

System.out.print(e.toString());

}

});

}

public String GETDataFrom(String endPointName) throws IOException {

Request request = new Request.Builder().url(httpUrl + endPointName).build();

try (Response response = client.newCall(request).execute()) {

return response.body().string();

}

}

public String POSTDataFrom(String endPointName, String postBody) throws Exception {

@SuppressWarnings(“deprecation”)

Request request = new Request.Builder().url(httpUrl + endPointName)

.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody)).build();

try (Response response = client.newCall(request).execute()) {

if (!response.isSuccessful()) {

throw new IOException(“Unexpected code “ + response);

} else {

return response.body().string();

}

}

}

/*

* whenAsynchronousGetRequest_thenCorrect see web site

* https://square.github.io/okhttp/recipes/#posting-a-string-kt-java

*

* FIX CORS ERROS LOOK THIS

*

* Request request = new Request.Builder()

* .url(“https://api.github.com/repos/square/okhttp/issues")

* .header(“User-Agent”, “OkHttp Headers.java”) .addHeader(“Accept”,

* “application/json; q=0.5”) .addHeader(“Accept”,

* “application/vnd.github.v3+json”) .build();

*

* 1- api response content type ckeck in postman

* 2- create model class to json

* response key names using

* 3- gson using deserialize data set to model class

* array object

*

*

*/

}

Create new java class

package modelsPkg;

public class CentersModel {

private String id, name, location = null;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getLocation() {

return location;

}

public void setLocation(String location) {

this.location = location;

}

}

--

--

--

BSC(HONS) COMPUTING AND SOFTWARE ENGINNERING. I MOST INTERETED TO BUILD CLONE, STREAMING PLATFORM , WEB -MOBILE-DESKTOP FULLSTACK SOFTWARE DEVELOPMENT.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building a robust GPU cloud in 60 mins Part 2. NVIDIA A100 and MIG on Kubernetes

Product lessons learned from our recent iPhone beta test

It’s happening — Sphere Hackathon 2021!

Sphere Hackathon 2021

How to extract zip files in S3 ?

Building Microservices (Part 2)

Why Golang Is Taking Over the Software Industry

The new three letter design pattern for the web that will take you beyond MVC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Savindu Pasintha

Savindu Pasintha

BSC(HONS) COMPUTING AND SOFTWARE ENGINNERING. I MOST INTERETED TO BUILD CLONE, STREAMING PLATFORM , WEB -MOBILE-DESKTOP FULLSTACK SOFTWARE DEVELOPMENT.

More from Medium

Nuisance of working with asynchronous calls

Creating a simple web server with node.js

About Redis with code