N
N
Nubbin2017-07-30 13:44:25
Android
Nubbin, 2017-07-30 13:44:25

Retrofit2 calls link 2 times?

Hi everybody.
I made a log on the site here and noticed that if I send data to the site once, retforit calls it 2 times, we didn’t encounter it.

07-30 10:35:27.301 21415-24513/com.project.test D/OkHttp: --> GET http://localhost/testing http/1.1
07-30 10:35:27.301 21415-24513/ru.project.test D/OkHttp: --> END GET
07-30 10:35:27.682 21415-24513/ru.project.test D/OkHttp: <-- 200 OK http://localhost/testing?code=1 (381ms)
07-30 10:35:27.683 21415-24513/ru.project.test D/OkHttp: Server: nginx
07-30 10:35:27.683 21415-24513/ru.project.test D/OkHttp: Date: Sun, 30 Jul 2017 10:35:28 GMT
07-30 10:35:27.683 21415-24513/ru.project.test D/OkHttp: Content-Type: application/json
07-30 10:35:27.683 21415-24513/ru.project.test D/OkHttp: Transfer-Encoding: chunked
07-30 10:35:27.683 21415-24513/ru.project.test D/OkHttp: Connection: keep-alive
07-30 10:35:27.683 21415-24513/ru.project.test D/OkHttp: Vary: Accept-Encoding

public class Api {
    private static Retrofit RETROFIT;
    public static Retrofit getRetrofit() {
        if (RETROFIT == null) {
            OkHttpClient okHttpClient = initHttpClient();
            RETROFIT = new Retrofit.Builder()
                    .baseUrl("http://172.16.0.22:8099")
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return RETROFIT;
    }

    private static OkHttpClient initHttpClient() {
        return new OkHttpClient.Builder()
                .addInterceptor(new LogInterceptor(BuildConfig.DEBUG))
.addInterceptor(new Interceptor()
                {
                    @Override
                    public Response intercept(Interceptor.Chain chain) throws IOException
                    {
                        Request original = chain.request();

                        HttpUrl originalHttpUrl = original.url();
                        HttpUrl url = originalHttpUrl.newBuilder()
                                .addQueryParameter("code", "1")
                                .build();

                        Request.Builder requestBuilder = original.newBuilder()
                                .url(url)
                                .method(original.method(), original.body());
                        Request request = requestBuilder.build();

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

    public static UserApi getUserApi() {
        return getRetrofit().create(UserApi.class);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2017-08-02
@YuryBorodkin

judging by the logs, retrofit makes 1 request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question