ANDROID - RETROFIT: Как справиться, когда у меня истекло время ожидания? - PullRequest
0 голосов
/ 21 февраля 2020

у меня есть 2 ссылки. Остальные api:

первичная ссылка: https://www.primarylink.com/api/ вторичная ссылка: https://172.11.12.211: 8221 / вторичная ссылка / api

Я хочу, если моя основная ссылка выходит из строя, android будет запрашивать дополнительную ссылку.

, но я не знаю, как внедрить эту логику c в код.

вот мой код:

    static OkHttpClient GET_OK_HTTP_CLIENT() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(5, MINUTES)
                .readTimeout(5, MINUTES)
                .writeTimeout(5, MINUTES);
        if (ALLOW_INSECURE) {
            builder.sslSocketFactory(GET_SSL_SOCKET_FACTORY(), GET_X509_TRUST_MANAGER());
            builder.hostnameVerifier(GET_HOSTNAME_VERIFIER());
        }

        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();

        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        builder.addInterceptor(new NetworkConnectionInterceptor());
        builder.addInterceptor(httpLoggingInterceptor);
        builder.addInterceptor(new AuthenticationInterceptor());

        builder.cookieJar(new CookieJar() {
            @Override
            public void saveFromResponse(@NonNull HttpUrl url, @NonNull List<Cookie> cookies) {
                COOKIES = cookies;
            }

            @Override
            public List<Cookie> loadForRequest(@NonNull HttpUrl url) {
                return COOKIES;
            }
        });

        return builder.build();
    }

Мой запрос на первичную ссылку:

  public static Retrofit GET_RETROFIT() {
        if (OK_HTTP_CLIENT == null) {
            OK_HTTP_CLIENT = GET_OK_HTTP_CLIENT();
        }

        if (RETROFIT == null) {
            RETROFIT = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(OK_HTTP_CLIENT)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create(
                            new GsonBuilder()
                                    .registerTypeAdapter(LocalDateTime.class, new CustomDateTimeDeserializer())
                                    .registerTypeAdapter(LocalDate.class, new CustomDateDeserializer())
                                    .registerTypeAdapter(LocalTime.class, new CustomTimeDeserializer())
                                    .registerTypeAdapter(LocalDateTime.class, new CustomDateTimeSerializer())
                                    .registerTypeAdapter(LocalDate.class, new CustomDateSerializer())
                                    .registerTypeAdapter(LocalTime.class, new CustomTimeSerializer())
                                    .create()
                            )
                    )
                    .addConverterFactory(new CustomConverterFactory())
                    .build();
        }

        return RETROFIT;
    }

Мой запрос на вторичную ссылку:

ublic static Retrofit RETROFIT_BACKUP() {
        if (RETROFIT == null) {
            RETROFIT = new Retrofit.Builder()
                    .baseUrl(Constant.URL.BASE_DEVELOPMENT_SECOND)
                    .client(OK_HTTP_CLIENT)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create(
                            new GsonBuilder()
                                    .registerTypeAdapter(LocalDateTime.class, new CustomDateTimeDeserializer())
                                    .registerTypeAdapter(LocalDate.class, new CustomDateDeserializer())
                                    .registerTypeAdapter(LocalTime.class, new CustomTimeDeserializer())
                                    .registerTypeAdapter(LocalDateTime.class, new CustomDateTimeSerializer())
                                    .registerTypeAdapter(LocalDate.class, new CustomDateSerializer())
                                    .registerTypeAdapter(LocalTime.class, new CustomTimeSerializer())
                                    .create()
                            )
                    )
                    .addConverterFactory(new CustomConverterFactory())
                    .build();
        }
        return RETROFIT;
    }
...