Thanee Stevens
Thanee Stevens

Reputation: 21

Retrofit call does executed but not trigger onResponse or onFailure

My call is executed but does not trigger the onResponse or onFailure method The Json object that is returned has an image inside

This is the code in my interactor class

 private ExamApi initiateRetrofit() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    return retrofit.create(ExamApi.class);
}

public void getExamImage(String user, String token,String exId, String exDocId) {
    if (examApi == null) initiateRetrofit();
    Call<ResponseBody> call = examApi.getExamImage(user,token,exId,exDocId);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            listener.onImageSuccess(response.body());
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            //TODO ONFAILURE
            //listener.onImageFailure();
           listener.onFailure();
        }
    });
}

this is my examApi

@GET("mobile/getExamImage")
Call<ResponseBody> getExamImage(
        @Query("user") String username,
        @Query("token") String token,
        @Query("exid") String exId,
        @Query("edid") String exDocId
);

Upvotes: 2

Views: 1408

Answers (1)

Bharath
Bharath

Reputation: 401

It is happening because you are making an asynchronous call on the retrofit. You are trying to capture the data even before the network call happens. This gives you a null result. Try Broadcasting the result using broadcast receiver or third party library like EventBus which will make your result available after the network call has happened.

Upvotes: 1

Related Questions