Retrofit

To use retrofit

add to buiul.gradle

    implementation 'com.squareup.retrofit2:retrofit:2.11.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.11.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'

Create Api interface

public interface ApiExample {
    @GET("public/data")
    Call<T> getAllData();
 
    // This will be public/data?dataId=yourdataId
    @GET("public/data")
    Call<T> getOneData(@Query("dataId"), String yourdataId);
}

Create Data Model

public class DataModel {
	private String name;
 
	public String getName() {
        return name;
    }
}
 
public class DataList {
	public List<DataModel> dataList;
}

Create Wrapper

public abstract class WrappedCallback<T> implements Callback<T> {
 
    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (response.isSuccessful()) {
           	//perform your own handler onSuccess
            this.onSuccess(response.body(), response)
        } else {
        	//perform your own handler onError
            this.onError()
        }
 
    }
 
    @Override
    public void onFailure(Call<T> call, Throwable throwable) {
        throwable.printStackTrace();
    }
 
    public abstract void onSuccess(T result, Response response);
 
    public abstract void onError(Response response);
}

Create client

public class HttpService{
 
    public static final String API_URL = "https://api.example.com";
 
    private Context context;
    private static Retrofit retrofit;
    private static HttpService instance;
    
    private HttpService(Context context) {
 
        this.context = context;
        if (retrofit == null) {
           	// Optional, set up log
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
            
            // retrofit instance
            retrofit = new Retrofit.Builder()
                    .baseUrl(API_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();
        }
    }
 
    public static HttpService getInstance(Context context) {
        if (instance == null) {
            instance = new HttpService(context);
        }
        return instance;
    }
 
    public void getOneData(String yourdataId, WrappedCallback<DataModel> callback) {
        ApiExample apiService = retrofit.create(ApiExample.class);
        Call<DataModel> call = apiService.getAllData(yourdataId);
        call.enqueue(callback);
    }
 
    public void getAllData(WrappedCallback<DataList> callback) {
        ApiExample apiService = retrofit.create(ApiExample.class);
        Call<DataList> call = apiService.getOneData();
        call.enqueue(callback);
    }
}

Use in other Activity/Fragment/WhatsoeverPlaces

HttpService ht = HttpService.getInstance(this.getContext());
 
ht.getOneData( "your data Id", new WrappedCallback<DataModel>() {
            @Override
            public void onSuccess(DataModel result, Response response) {
                // Do something with one result
            }
 
            @Override
            public void onError(Response response) {
            	// Handdle error
 
            }
        });