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"
}
{
"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!"
}
}
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;
}
}
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;
}
}
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);
}
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.