How to setup Retrofit with no baseUrl

From source (New URL resolving concept) you can simply specify whole path in post request. Moreover we also can declare a full URL in @Post in Retrofit 2.0: public interface APIService { @POST(“http://api.nuuneoi.com/special/user/list”) Call<Users> loadSpecialUsers(); } Base URL will be ignored for this case.

Use Retrofit methods more expressive way

this is how i do it with extension function and a class fun<T> Call<T>.enqueue(callback: CallBackKt<T>.() -> Unit) { val callBackKt = CallBackKt<T>() callback.invoke(callBackKt) this.enqueue(callBackKt) } class CallBackKt<T>: Callback<T> { var onResponse: ((Response<T>) -> Unit)? = null var onFailure: ((t: Throwable?) -> Unit)? = null override fun onFailure(call: Call<T>, t: Throwable) { onFailure?.invoke(t) } override fun … Read more

Retrofit2: Modifying request body in OkHttp Interceptor

I using this to add post parameter to the existing ones. OkHttpClient client = new OkHttpClient.Builder() .protocols(protocols) .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request.Builder requestBuilder = request.newBuilder(); RequestBody formBody = new FormEncodingBuilder() .add(“email”, “Jurassic@Park.com”) .add(“tel”, “90301171XX”) .build(); String postBodyString = Utils.bodyToString(request.body()); postBodyString += ((postBodyString.length() > 0) … Read more

How to create a call adapter for suspending functions in Retrofit?

Here is a working example of an adapter, which automatically wraps a response to the Result wrapper. A GitHub sample is also available. // build.gradle … dependencies { implementation ‘com.squareup.retrofit2:retrofit:2.6.1’ implementation ‘com.squareup.retrofit2:converter-gson:2.6.1’ implementation ‘com.google.code.gson:gson:2.8.5’ } // test.kt … sealed class Result<out T> { data class Success<T>(val data: T?) : Result<T>() data class Failure(val statusCode: Int?) … Read more