Retrofit Tips

Retrofit Tips

  • Ok http interceptor for changing the base url in tests

/** * An interceptor that allows runtime changes to the URL hostname. Usually used in combination with MockWebServer. */ publicfinalclassBaseUrlInterceptorimplementsInterceptor{ @NullableprivatevolatileString host; privatefinalString realBaseUrl; publicBaseUrlInterceptor(String realBaseUrl) { this.realBaseUrl = realBaseUrl; } publicvoidsetBaseUrl(String host) { this.host = host; } publicvoidresetBaseUrl() { this.host = realBaseUrl; } @Override publicResponseintercept(Chain chain)throwsIOException { Request request = chain.request(); if(host !=null&& !realBaseUrl.equals(host)) { @NullableHttpUrl newUrl = HttpUrl.parse(host); request = request.newBuilder() .url(newUrl) .build(); } returnchain.proceed(request); } }

  • Converters: Use the same converter instance over multiple Retrofit clients:

  • Converters: You can add multiple Converter Factories (json + protocol buffer) to Retrofit (order matters):

  • Converters: When having Envelop of Respones (Server metadata + response body) and you want to unwrap it:

  • Call Adapters: Convert Call response to any type e.g RxJava Observables:

  • Call Adapters: Serializing Error Response bodies (Error 500’s/400’s):

  • Mock Mode

Moshi:

results matching ""

    No results matching ""