有时Retrofit 2.1.0回调不起作用

时间:2016-09-21 06:44:22

标签: android retrofit2

回调在某些屏幕上有时不起作用,而很多时候它正在完美运行。我几乎完成了这个库的项目。亲切的帮助

我的ApiClient代码是:

public class ApiClient {
    public static final String BASE_URL = AppUtils.MainURL;
    private static Retrofit retrofit = null;

    public static Retrofit getClient(final String token) {
        OkHttpClient defaultHttpClient = new OkHttpClient.Builder()
                .addInterceptor(
                        new Interceptor() {
                            @Override
                            public Response intercept(Interceptor.Chain chain) throws IOException {
                                Request request = chain.request().newBuilder()
                                        .addHeader("AccessToken", token).build();
                                return chain.proceed(request);
                            }
                        }).retryOnConnectionFailure(true).connectTimeout(1, TimeUnit.MINUTES)
                .readTimeout(1, TimeUnit.MINUTES)
                .writeTimeout(1, TimeUnit.MINUTES).build();

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

并且调用方法是:

 ApiInfo apiService =
                ApiClient.getClient(preference.getToken()).create(ApiInfo.class);
        Call<JobDetailsResponse> responseCall = apiService.getJobDetails(preference.getLoginId(), preference.getToken(), "" + jobId);

        responseCall.enqueue(new Callback<JobDetailsResponse>() {
            @Override
            public void onResponse(Call<JobDetailsResponse> call, Response<JobDetailsResponse> response) {
                progressDialog.dismiss();
                JobDetailsResponse jobDetailsResponse = response.body();
            }

            @Override
            public void onFailure(Call<JobDetailsResponse> call, Throwable t) {

                progressDialog.dismiss();
            }
        });

0 个答案:

没有答案