TheJadav

android retrofit example

Make your first API call using retrofit

Add retrofit and GsonConverter in your project

why to use GsonConverter here?. It will be useful for converting JsonObject directly to Java class or Kotlin class. So, we only need to pass our models.

implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
implementation 'com.google.code.gson:gson:2.8.5'

Make your first API call using retrofit

Add retrofit and GsonConverter in your project

why to use GsonConverter here?. It will be useful for converting JsonObject directly to Java class or Kotlin class. So, we only need to pass our models.

implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
implementation 'com.google.code.gson:gson:2.8.5'

Single instance object retrofit

What it means by single instance? We will use single instance in whole project.

   fun getRetrofitInstance() : Retrofit{
        if(retrofit == null){
            retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }
        return retrofit!!
    }

Create interface for Api calling. 

This will include modelclass for login as well as response for api call

interface ApiInterface {
    @POST("LoginApi/LoginSubmit")
    fun login(@Body params: LoginParams) : Call
}

Call Api with retrofit

this is final step. You need to create instance of ApiInterface with retrofit and make api call.

        val apiInterface = ApiManager.getClient().create(ApiInterface::class.java)
        val call = apiInterface.login(LoginParams(email, password))


        //For background api call
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, t: Throwable) {
                //handle failure
            }

            override fun onResponse(call: Call, response: Response) {
                //Enjoy success
                //response.body have your LoginResponse object
            }
        })

       //For same thread api call
       val response  = call.execute()

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message