

In my examples, I will be using version: 2.9.0. Moreover, depending on when you are reading this article, the latest stable version can be different and I recommend using the latest one. It’s worth mentioning, that Retrofit requires at minimum Java 8+ or Android API 21+, so it won’t be compatible with the legacy codebase.

If you are looking for a library, which will help you to integrate external API in your mobile application, or a back-end service written in Java or Kotlin, then Retrofit is definitely worth a try. Let’s start by answering the question: what exactly the Retrofit is? It is a type-safe HTTP client for Android and Java. I will walk you step by step through its features, capabilities and a few obstacles you may encounter when using it. Println("Error parse json from intercept.This time, I would like to show you how to use Retrofit 2 with Kotlin. Val myJson = resp.peekBody(2048).string() // peekBody() will not close the response Val resp = chain.proceed(chain.request()) connectTimeout(TIME_OUT, TimeUnit.SECONDS) Private val okHttpClient = OkHttpClient.Builder() Private val gson = GsonBuilder().setLenient().create() Private external fun baseUrlFromJNI(boolean: Boolean): StringĬlass Failure(val e: Throwable) : ApiState()Ĭlass Success(val data: Any) : ApiState() Now make a new package, recommended name is: “networkService”

kt files in a zip file.Ĥ.Ğxtract zip file and place converted. Get json string from your api response.ģ.Ĝopy and paste the json to web site and download converted. Implementation(":kotlinx-coroutines-android:1.4.3")Ģ. Implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1") Implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1") Implementation("androidx.lifecycle:lifecycle-extensions:2.2.0") Implementation("3:logging-interceptor:4.4.0") // for cache How do you make a network call with retrofit, GSON and Coroutine in MVVM pattern in your Android project?ġ.Ědd Below all libraries(Gradle KTS), If you are using gradle make some chage. Var mUser = omJson(myJson, UserData::class.java) GSON is not for only Android you can use it in your any Kotlin or JAVA project. You just can use GSON which is developed by google. Which library should I use for data parsing in Kotlin?
