为什么Retrofit的onResponse方法没有运行?

时间:2018-06-29 07:18:05

标签: android retrofit

我正在使用Retrofit库从woocommerce网站获取JSON数据。

但是onResponse方法未运行。 我知道为什么该方法不起作用。

这是我的代码:

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");
            }
        });

这是我的API客户端类:

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;
}

}

为什么onResponse方法不起作用并跳过。 在这种情况下,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

尝试将此更改更改为api客户端类,然后按如下所示进行调用。

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;
}

}

像下面的代码一样调用api。

 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类是具有服务器响应的自定义pojo类。

并调用处理所有api的api make接口。

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