N
N
Nubbin2017-07-30 19:45:37
Android
Nubbin, 2017-07-30 19:45:37

Retrofit2 one processing is done 2 times?

Good evening, I just can not understand one thing.
Connect Retforit2 with an additional okHttp client, connect to a button that, when pressed, should add an entry to the database, when I click once it hits the database 2 times, even to the "php" file from which it is processed and added connected the log, it also completed it 2 times . What could be the problem here, help me, yes, for 3 hours I've been scratching my head at the code below.
Here it calls the file 2 times, I don't know why.

07-30 10:35:27.301 21415-24513/ri.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);
    }
}

Call<Void> call = UserApi.getUserApi().postCreateContact("phone","7****");
                call.enqueue(new Callback<Void>()
                {
                    @Override
                    public void onResponse(@NonNull Call<Void> call, @NonNull Response<Void> response)
                    {
//
          }
        });

Guys please help me what is the problem.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question