This article explains about calling Web API request in Android application using Java. Also will see some Web API concepts like HTTP Methods, Status codes, Headers, requests, and responses. Mainly Web API calls are one of the important parts of application development.
- Web API: Web-based Application Programming Interface is used for integrating two different Systems like Data transactions between the Database server and client-side server with the use of HTTP requests and responses over the web browser.
- HTTP Client: HTTP (Hypertext Transfer Protocol) Client library& is used to send a request and get a response from Web servers using GET, POST, Put, and delete HTTP Methods.
- Header: The header contains information such as the content type, authorization details, request and response of HTTP metadata
- Status code: Status code is used to indicate whether a web API request is a success or failure. Status code: 200 is a Successful response, Status code: 500 is an Internal Server, and Error Status code: 400 is a Bad Request.
Now look at the code example of an API call in Android using Java. Android provides a dependency library called 'okhttp3' to request the web API responses.
dependencies{
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
}
For requesting API from internet access or permission should be provided from the AndroidManifest.xml File.
<uses-permission android:name="android.permission.INTERNET" />
Java Code
OkHttpClient client = new OkHttpClient();
String url="https://reqres.in/api/users%20page=2";
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String myResponse = response.body().string();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
txtView.setText(myResponse);
}
});
}
});
XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:orientation="vertical"
tools:context=".MainActivity"
tools:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:background="#3CA74A"
android:orientation="vertical">
<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="API Response" />
<EditText
android:id="@+id/titleEditText"
android:layout_width="wrap_content"
android:background="@drawable/edittext_bg"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="@color/black"
android:hint="Title"/>
</LinearLayout>
</LinearLayout>
Output