Transformation 2 team entry method and operation for 2 times

I'm new to renovating the library. I'm developing an application that must make multiple API calls, but this problem bothers me when I try to make the first API call

The problem I face is that whenever I used to call the modified asynchronous call method, the function in the onresponse method will run twice

This is the code when I call the API asynchronously

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

After running the application on the device, when I see my android studio recorder, it will display log messages to me-

E/mytag: This is running
E/mytag: This is running

It seems to have run twice here!

I don't understand why it runs twice. Can you help me with this

Just for more help... I've implemented such code

Apimodule interface (where my API call URL is defined)

public abstract interface ApiModule {

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

}

Apiservicegenerator like this-

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

resolvent:

Finally I solved my problem. This is not a problem of renovating the library!!

In fact, this is my bad thing. I opened the fragment twice (I don't know before answering this question)... That's why the code in the fragment runs twice, which makes me think that the transformation response runs twice

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>