DaVoD Hoseiny
DaVoD Hoseiny

Reputation: 141

Why the onResponse method of Retrofit didn't run?

I am using Retrofit library for get JSON data from woocommerce site.

But the onResponse method isn't running. I have know idea why the method not working .

here is my code:

APIService service = APIClient.getClient().create(APIService.class);
Call<DataModelObjectOfProducts> productsCall = service.getObjectOfProducts();

        productsCall.enqueue(new Callback<DataModelObjectOfProducts>() {
            @Override
            public void onResponse(Call<DataModelObjectOfProducts> call, Response<DataModelObjectOfProducts> response) {

                object_of_products = response.body();
                products = object_of_products.getProducts();


                //recycler most popular
                mostPopularProductShopMainPageAdapter = new ProductShopMainPageAdapter(getActivity(), products);
                recyclerViewMostPopularProductShopMainPage.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
                recyclerViewMostPopularProductShopMainPage.setAdapter(mostPopularProductShopMainPageAdapter);


            }

            @Override
            public void onFailure(Call<DataModelObjectOfProducts> call, Throwable t) {
                Log.e("TAG", "onFailure: error");
            }
        });

and here is my API Client class:

public class APIClient {

public static final String BASE_URL = "https://4nal.ir/wc-api/v3/";
public static final String CONSUMER_KEY = "ck_ce4614d220f71ec1314a9ebef2d5ad60";
public static final String CONSUMER_SECRET = "cs_8a2117a28e69eb3c8d490522bb9c9e83";
private static Retrofit retrofit = null;

public static Retrofit getClient(){

    OkHttpClient httpClient = new OkHttpClient.Builder()
            .readTimeout(20, TimeUnit.SECONDS)
            .connectTimeout(20,TimeUnit.SECONDS)
            .build();


    if (retrofit == null){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(httpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    return retrofit;
}

}

why the onResponse method not working and skip. any one can help me in this case ?

Upvotes: 0

Views: 719

Answers (1)

user4571931
user4571931

Reputation:

Try this change into api client class and call like below..

public class ApiClient {
private final static String BASE_URL = "https://api.github.com";

public static ApiClient apiClient;
private Retrofit retrofit = null;

public static ApiClient getInstance() {
    if (apiClient == null) {
        apiClient = new ApiClient();
    }
    return apiClient;
}

//private static Retrofit storeRetrofit = null;

public Retrofit getClient() {
    return getClient(null);
}


private Retrofit getClient(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            return chain.proceed(request);
        }
    });

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


    return retrofit;
}

}

calling api like below code..

 ApiInterface apiInterface = ApiClient.getInstance().getClient().create(ApiInterface.class);
    Call<ResponseData> responseCall = apiInterface.getdata();
    responseCall.enqueue(new Callback<ResponseData>() {
        @Override
        public void onResponse(Call<ResponseData> call, retrofit2.Response<ResponseData> response) {
            if (response.isSuccessful() && response.body() != null && response != null) {
                Toast.makeText(getApplicationContext(), "GetData" + response.body().getLanguage(), Toast.LENGTH_SHORT).show();


            }
        }

        @Override
        public void onFailure(Call<ResponseData> call, Throwable t) {
            Log.d("Errror", t.getMessage());
        }
    });

ResponseData class is custom pojo class that have server response..

and call api make interface that handle all apis..

public interface ApiInterface {
@GET("/users/waadalkatheri/repos")
Call<Response> getdata();
}

Upvotes: 1

Related Questions