[Solved] Creating Requests to an API in Java


Ok first open a gradle project and add these dependencies:

    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

Then make an interface for api calls:

I have created a dummy for you:

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

import java.util.List;

public interface Api {
    @GET("/state")
    Call<List<String>> groupList(@Query("id") int groupId);
}

Then add another class for retrofitclient:

import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {

    private static RetrofitClient mInstance;
    private final Retrofit retrofit;

    private RetrofitClient()
    {
        final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(90, TimeUnit.SECONDS)
                .writeTimeout(90, TimeUnit.SECONDS)
                .connectTimeout(90, TimeUnit.SECONDS)
                .build();


        //your base url with port number goes here
        String baseUrl = "http://192.168.0.2/";

        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();
    }

    public static synchronized RetrofitClient getInstance()
    {
        if(mInstance == null)
        {
            mInstance = new RetrofitClient();
        }
        return mInstance;
    }

    public Api getApi()
    {
        return retrofit.create(Api.class);
    }
}

Now final step checking the outcomes. A dummy class is added by me for you:

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        RetrofitClient.getInstance().getApi()
                .groupList(50)
                .enqueue(new Callback<List<String>>() {
                    @Override
                    public void onResponse(Call<List<String>> call, Response<List<String>> response) {
                        if(response.isSuccessful())
                        {
                            //do what ever you want to do
                        }
                        else
                        {
                            //failed
                        }
                    }

                    @Override
                    public void onFailure(Call<List<String>> call, Throwable t) {
                        //Failed to fetch data
                    }
                });
    }
}

I am also adding some screenshots for your help:

Gradle dependencies:

Gradle Dependencies

Api interface:

Api Interface

RetrofitClient class:

RetrofitClient

Main class for demonstration:

Main class

6

solved Creating Requests to an API in Java