Native Mobile App Development With Java

Android Retrofit Example Java

15-Sep-2024

Learn how to use Retrofit to handle API requests in Android Studio Project using Java


We are Get data from https://reqres.in/api/users/2 this url

1/  Create A New Project and add Libarary.




dependencies {

implementation libs.appcompat
implementation libs.material
implementation libs.activity
implementation libs.constraintlayout
testImplementation libs.junit
androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core

implementation "com.squareup.retrofit2:converter-gson:2.8.1" //Add Library here
implementation "com.squareup.retrofit2:retrofit:2.9.0"
}



2/ See  Api Response  




{
"data": {
"id": 2,
"email": "janet.weaver@reqres.in",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
},
"support": {
"url": "https://reqres.in/#support-heading",
"text": "To keep ReqRes free, contributions towards server costs are appreciated!"
}
}





3/  Create A Model Class User.class





package com.example.retrofitexample;

public class User {
private int id;
private String first_name;
private String last_name;
private String email;
private String avatar;

public User(int id, String first_name, String last_name, String email, String avatar) {
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.avatar = avatar;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFirst_name() {
return first_name;
}

public void setFirst_name(String first_name) {
this.first_name = first_name;
}

public String getLast_name() {
return last_name;
}

public void setLast_name(String last_name) {
this.last_name = last_name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getAvatar() {
return avatar;
}

public void setAvatar(String avatar) {
this.avatar = avatar;
}
}





4/  Create A New Class UserResponse





package com.example.retrofitexample;

public class UserResponse {
private User data;

public UserResponse(User data) {
this.data = data;
}

public User getData() {
return data;
}

public void setData(User data) {
this.data = data;
}
}





5/  Create A Interface RetrofitAPI.






package com.example.retrofitexample;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface RetrofitAPI {
@GET("api/users/{id}")
public Call<UserResponse> getUser(@Path("id") int id);
}




6/  Now  in MainActivity





package com.example.retrofitexample;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

TextView email;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
email=findViewById(R.id.email);
getData();
}
private void getData(){
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://reqres.in/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitAPI retrofitAPI = retrofit.create(RetrofitAPI.class);
Call<UserResponse> call = retrofitAPI.getUser(2);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
email.setText(response.body().getData().getEmail()+"");
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
Toast.makeText(MainActivity.this, "Fail to get the data..", Toast.LENGTH_SHORT).show();
}
});
}
}



Run code then see output.



Comments