Retrofit api configuration in android

 Step 1 – Adding retrofit to our application

Add the following dependencies to your app-level build.gradle file.

//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation("com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.5")

 

Step 2 – Add Internet Permission (Manifest)

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

 Add also this line in <application>

android:usesCleartextTraffic="true"


Step 3 – Create a retrofit instance

Add the following lines of code to the ApiClient.java:

public class ApiClient {

public static Retrofit getRetrofit() {

HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(300, TimeUnit.SECONDS)
.readTimeout(300, TimeUnit.SECONDS)
.writeTimeout(300, TimeUnit.SECONDS)
.addInterceptor(httpLoggingInterceptor).build();

return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("your base url")
.client(okHttpClient)
.build();
}

public static UserService getUserService() {

return getRetrofit().create(UserService.class);
}
}


Step 4 – Create a retrofit instance

Add the following lines of code to the UserService.java:

public interface UserService {
@GET("GETById")
Call<UserResponse>
GetById(@Header("UserId") int UserId);


Step 5 – Create a Model Class

Add the following lines of code to the UserResponse.java:

public class UserResponse {

@SerializedName("UserId")
@Expose
private Integer userId;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("IpAdress")
@Expose
private Object ipAdress;
@SerializedName("IsEmailVerified")
@Expose
private Boolean isEmailVerified;
@SerializedName("Email")
@Expose
private String email;
@SerializedName("Mobile")
@Expose
private String mobile;
@SerializedName("Gender")
@Expose
private String Gender;
@SerializedName("Message")
@Expose
private String message;
@SerializedName("StepDone")
@Expose
private String stepDone;
@SerializedName("EventTypeId")
@Expose
private String eventTypeId;
@SerializedName("EventId")
@Expose
private String eventId;
@SerializedName("EventRelationId")
@Expose
private Integer eventRelationId;

public Integer getUserId() {
return userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Object getIpAdress() {
return ipAdress;
}

public void setIpAdress(Object ipAdress) {
this.ipAdress = ipAdress;
}

public Boolean getIsEmailVerified() {
return isEmailVerified;
}

public void setIsEmailVerified(Boolean isEmailVerified) {
this.isEmailVerified = isEmailVerified;
}

public String getEmail() {
return email;
}

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

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public String getGender() {
return Gender;
}

public void setGender(String gender) {
Gender = gender;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getStepDone() {
return stepDone;
}

public void setStepDone(String stepDone) {
this.stepDone = stepDone;
}

public String getEventTypeId() {
return eventTypeId;
}

public void setEventTypeId(String eventTypeId) {
this.eventTypeId = eventTypeId;
}

public String getEventId() {
return eventId;
}

public void setEventId(String eventId) {
this.eventId = eventId;
}

public Integer getEventRelationId() {
return eventRelationId;
}

public void setEventRelationId(Integer eventRelationId) {
this.eventRelationId = eventRelationId;
}
}


Step 5 – Api Handle in MainActivity

Add the following lines of code to the MainActivity.java:

Call<UserResponse> call = ApiClient.getUserService().GetById(id);
call.enqueue(new Callback<UserResponse>() {
@SuppressLint("SetTextI18n")
@Override
public void onResponse(@NotNull Call<UserResponse> call, @NotNull Response<UserResponse> response) {
UserResponse userResponse = response.body();
if (userResponse != null) {

}
}

@Override
public void onFailure(@NonNull Call<UserResponse> call, @NonNull Throwable t) {

}
});


That is it!

Let’s run our application.


Previous Post Next Post

Contact Form