retrofit api get request not firing the calback methods

Viewed 47

I'm encountering a problem with retrofit : Im trying to make a get request to this API: https://test.spaceflightnewsapi.net/api/v2/articles

But somehow the callbacks methods from the retrofit are not called. I get this in the logcat/ the only thing related to the retrofit (okkhttp):

 I/okhttp.OkHttpClient: --> GET https://test.spaceflightnewsapi.net/api/v2/articles
 I/okhttp.OkHttpClient: --> END GET

here is my api interface:

public interface SpaceNewsApi {
    String BASE_URL = "https://test.spaceflightnewsapi.net/api/v2/";
    @GET("articles")
    Call<List<SpaceNews>> getSpaceNewsArticles();
}

here is the retrofit client (note that i will be using two apis with the retrofit this is why i'm using two methods to build it), also i've added a loginginterceptor, I've change the Level to different types but only the things from above are the messages i get each time in the logs related to retrofit

public class RetrofitClients {
    private static RetrofitClients instance = null;
    private SpaceNewsApi mySpaceNewsApi = null;
    private LaunchLibraryApi myLaunchLibraryApi = null;

    public static synchronized RetrofitClients getInstance() {
        if (instance == null) {
            instance = new RetrofitClients();
        }
        return instance;
    }

    public SpaceNewsApi getMySpaceNewsApi() {
        if(mySpaceNewsApi == null) {
            Retrofit retrofit = new Retrofit.Builder().baseUrl(SpaceNewsApi.BASE_URL)
                    .client(getHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            mySpaceNewsApi = retrofit.create(SpaceNewsApi.class);
        }
        return mySpaceNewsApi;
    }

    public  LaunchLibraryApi getMyLaunchLibraryApi(){
        if(myLaunchLibraryApi == null) {
            Retrofit retrofit = new Retrofit.Builder().baseUrl(LaunchLibraryApi.BASE_URL)
                    .client(getHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            myLaunchLibraryApi = retrofit.create(LaunchLibraryApi.class);
        }
        return myLaunchLibraryApi;
    }

    public static OkHttpClient getHttpClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        //TODO : remove logging interceptors as it is to be used for development purpose
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(300, TimeUnit.SECONDS)
                .readTimeout(300, TimeUnit.SECONDS)
                .addInterceptor(logging)
                .build();
        return client;
    }
} 

I'm calling all of these in my main activity onCreate method like this:

 //testing Api Call
        Call<List<SpaceNews>> call = RetrofitClients.getInstance().getMySpaceNewsApi().getSpaceNewsArticles();
        call.enqueue(new Callback<List<SpaceNews>>() {

            @Override
            public void onResponse(Call<List<SpaceNews>> call, Response<List<SpaceNews>> response) {
                Log.d("gotApiData","got it");

            }

            @Override
            public void onFailure(Call<List<SpaceNews>> call, Throwable t) {
                Log.d("gotError","not got it");
                t.printStackTrace();
            }

        });

How can I get this working, I would appreciate any advice!

1 Answers

The problem I believe was with the RetrofitClients class, after adding a private constructor and moving the majority of code from the get***Api methods seems to solve the problem.

here is the modified RetrofitClients:

public class RetrofitClients {
    private static RetrofitClients instance = null;
    private SpaceNewsApi mySpaceNewsApi = null;
    private LaunchLibraryApi myLaunchLibraryApi = null;

    private RetrofitClients(){
        if(mySpaceNewsApi == null) {
            Retrofit retrofit = new Retrofit.Builder().baseUrl(SpaceNewsApi.BASE_URL)
                    .client(getHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            mySpaceNewsApi = retrofit.create(SpaceNewsApi.class);
        }
        if(myLaunchLibraryApi == null) {
            Retrofit retrofit = new Retrofit.Builder().baseUrl(LaunchLibraryApi.BASE_URL)
                    .client(getHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            myLaunchLibraryApi = retrofit.create(LaunchLibraryApi.class);
        }
    }

    public static synchronized RetrofitClients getInstance() {
        if (instance == null) {
            instance = new RetrofitClients();
        }
        return instance;
    }

    public SpaceNewsApi getMySpaceNewsApi() {

        return mySpaceNewsApi;
    }

    public  LaunchLibraryApi getMyLaunchLibraryApi(){

        return myLaunchLibraryApi;
    }

    public static OkHttpClient getHttpClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        //TODO : remove logging interceptors as it is to be used for development purpose
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(300, TimeUnit.SECONDS)
                .readTimeout(300, TimeUnit.SECONDS)
                .addInterceptor(logging)
                .build();
        return client;
    }
}
Related