July 10, 2018

How to make an API call in android Kotlin using retrofit library

     On this blog I am going to explain How to make an API call with the Help of Retrofit Library on Kotlin. Here is the steps to integrated API call using retrofit library in android Kotlin. With the help of and Interface class I am calling this API.


        First you need to add the dependency library on your app level build.gradle file.

compile 'com.google.code.gson:gson:2.8.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile('com.squareup.retrofit2:retrofit:2.1.0') {
    exclude module: 'okhttp'
}
compile 'com.squareup.retrofit2:converter-gson:2.2.0'


I am having a Constants.java class there I defiled my Base Url of my API like,


class Constants {
    companion object AppConstants {
        const val BaseUrl = "http://api.shidhin.net/";
    }
}


                      Next Step, I Created a new class ApiClient.java class, which we are used to invoke the API Call. Here is My ApiClient.java class,


class ApiClient {
    companion object {
        fun create(): ApiInterface {
            val TIMEOUT:Long = 2 * 60 * 1000
            val retrofit = Retrofit.Builder()
                    .client(OkHttpClient().newBuilder().connectTimeout(TIMEOUT, TimeUnit.SECONDS).readTimeout(TIMEOUT, TimeUnit.SECONDS).writeTimeout(TIMEOUT, TimeUnit.SECONDS).build())
                    .addConverterFactory(GsonConverterFactory.create()).baseUrl(Constants.AppConstants.BaseUrl)
                    .build()
            return retrofit.create(ApiInterface::class.java)
        }
    }

    val apiServices by lazy {
        ApiClient.create()
    }

}


                                  Next Step, I Created a new Interface ApiInterface.java , which which will  helps to API Call.  On this Class we will define the API functions. On this I have POST method with Header value, Here is My ApiInterface.java class,


interface ApiInterface {

    @POST("api/shops/AddNewShopDetails")
    @Headers("Content-Type: application/json")
    fun addShopDetails(@Body  body: JsonObject): Call<CommonServiceResponse>
}


       For the above API calls we used a class  CommonServiceResponse to parse the API response, Here is those classes, Based on the API response the structure of these class can be changed,

Class to parse addShopDetails API

class CommonServiceResponse {
    @SerializedName("status")
    public var status: Boolean? = null
    @SerializedName("message")
    public var message: String? = null
}

                    Now we added all the Class files we needed to the API Integration. Now I will show you How to invoke API calls on our Application.







API Call:  addShopDetails


val request = JsonObject()
request.addProperty("token", token)
request.addProperty("shopName", shopName)
request.addProperty("shopAddress", shopAddress)
ApiClient().apiServices.addShopDetails(request).enqueue(object : Callback<CommonServiceResponse> {
    override fun onResponse(call: Call<CommonServiceResponse>?, response: Response<CommonServiceResponse>?) {
        if (response != null && response.body() != null ) {
            
             //Here Comes the code to parse the result
} } override fun onFailure(call: Call<CommonServiceResponse>?, t: Throwable?) { Log.v("ApiFailure", "api Call Failure") } })
---------------
Thanks For Reading, Wish you a Happy Coding....

No comments:

Post a Comment