繁体   English   中英

Retrofit 2 enqueue 方法运行2次

[英]Retrofit 2 enqueue method running 2 times

我是 Retrofit Library 的新手。 我正在开发一个应用程序,我必须在其中进行多个 API 调用,但是当我尝试进行第一个 API 调用时,这个问题一直困扰着我......

我面临的问题是,每当我曾经调用改造的异步调用方法时,onResponse 方法中的功能就会运行 2 次...

这是我异步调用 API 时的代码...

final ApiModule apiService = ApiServiceGenerator.createService(ApiModule.class);
Call <ConfigResponse> call = apiService.getConfig();

call.enqueue(new Callback<ConfigResponse>() {
  @Override
  public void onResponse(Call<ConfigResponse> call, Response<ConfigResponse> response) {
    try {
        if (response.isSuccessful()) {
            Log.e("MyTag", "This is running");
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
  }

  @Override
  public void onFailure(Call<ConfigResponse> call, Throwable t) {
    e.printStackTrace();
  }
});

一旦我在设备上运行应用程序,当我看到我的 android studio 的记录器时,它就会向我显示日志消息 -

E/MyTag: This is running
E/MyTag: This is running

似乎在这里运行了 2 次.. !!

我不明白为什么它会运行 2 次。 请帮我解决这个...

只是为了更多帮助......我已经实现了这样的代码。

ApiModule 接口(我在这里定义了我的 API 调用 URL)

public abstract interface ApiModule {

  @GET("config")
  Call<ConfigResponse> getConfig();

}

ApiServiceGenerator 是这样的 -

public class ApiServiceGenerator {

public static final String API_BASE_URL = "https://www.example.com/";
private static OkHttpClient httpClient = new OkHttpClient.Builder()
        .addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request newRequest = chain.request().newBuilder().addHeader("App-Secret", "some-secret-key").build();
                return chain.proceed(newRequest);
            }
        })
        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) // Just For logging
        .readTimeout(60, TimeUnit.SECONDS)
        .connectTimeout(60, TimeUnit.SECONDS)
        .build();

Gson gson = new GsonBuilder()
        .registerTypeAdapterFactory(new ArrayAdapterFactory())
        .create();

private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create()));

public static <S> S createService(Class<S> serviceClass) {
    Retrofit retrofit = builder.client(httpClient).build();
    return retrofit.create(serviceClass);
}

public static Retrofit retrofit() { // For Error Handing when non-OK response is received from Server
    OkHttpClient httpClient = new OkHttpClient.Builder().build();
    OkHttpClient client = httpClient;
    return builder.client(client).build();
}
}

最后我解决了我的问题..这不是改造库的问题..!!

其实是我的不好。 我打开了两次片段(在回答这个问题之前我不知道)......这就是为什么片段中的代码运行了两次,这让我认为改造响应运行了两次......

就我而言,我使用了拦截器,在其中一个拦截器中我调用了 chain.proceed() 两次。 也许你也应该检查一下。 这不会出现在您的日志中。 使用 Stetho 来准确检查呼叫的次数。

不要从改造中调用任何在任何拦截器中多次返回“响应”的函数。

在我的情况下,由于在同一个拦截器中调用了两次 chain.proceed(request) ,它被调用了两次

例如

class ErrorInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {

        val request: Request = chain.request()
        chain.proceed(request)
        val response = chain.proceed(request) // it called twice in this Interceptor
        when (response.code()) {
            

        }
        return response
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM