googleads
how To Use Retrofit On android With Kotlin | Mobile App Development
preloder
Android Mobile App Development

How To Use Retrofit On android With Kotlin

 

Kotlin

Kotlin is a statically typed programming language and it’s runs on the Java virtual machine and also can be compiled to JavaScript source code or use the LLVM compiler infrastructure. It’s developed by the team of JetBrains programmers based in Saint Petersburg, Russia. 

What Is Retrofit

Retrofit is a REST Client for Java and Android. It sets easy way to retrieve and upload JSON or other structured data via REST based web service. Retrofit automatically serializes the JSON response using a POJO which must be defined in advanced structure on JSON.

Using Retrofit

We have to follow three main classes in retrofit,

  • Model class which is used as a JSON model
  • Interfaces that define the HTTP operations
  • Retrofit Client class

We need to add the following dependencies in our Build.gradle file,

  • compile “com.squareup.retrofit2:retrofit:2.3.0”
  • compile “com.squareup.retrofit2:converter-gson:2.3.0”
  • compile ‘com.squareup.okhttp3:logging-interceptor:3.10.0’

 

API Interface

We need a java interface to make an API call from retrofit, in this API interface we have to define all the URLs with the http request type and parameters. 

Here i have listed few type of request details.

Methods To Create API Interface

We should keep all the requests in a separate interface which are needs to be called in backend,

 

Post Request

 

Put Request

 

Delete Request 

 

Get Request

 

Query parameters also added with the @Query annotation on a method parameter. They are automatically added at the end of the URL.

 

Method used to add the standard backend URL or end point,

//**App Utils**

 

Retrofit Interface

Here we added the application interceptor with header details.

  • To register an application interceptor, we need to call addInterceptor() on OkHttpClient.Builder.
  • To register a Network Interceptor, invoke addNetworkInterceptor() instead of addInterceptor()

 

How To Add API interface into Retrofit Interface,

 

The getClient() method will be called every time while setting up a Retrofit interface. Retrofit provides with a list of annotations for each of the HTTP methods: @GET, @POST, @PUT, @DELETE.

Tips & Tricks:  Reduce Overdraw From Your Android Application

 

Sample Login Code For Kotlin Using Retrofit

Pass the username and password through json format, json is a lightweight data-interchange format.

XML Code

 

Kotlin Code

 

Validation Code

 

Best To Read:  How To Improve The Quality Of Android Apps Using Firebase Test Lab

 

Post Request Using Retrofit

 

The above Android P version requires small changes to do in manifest file because this version google also added the network security feature. So, incase if you don’t want to get blocked by the network security then you can simply add the below code in application tag (manifest) and set the value to be true,

 

Retrofit is the powerful network library that can easily connect to HTTP-based API services from your Android App and it will greatly help us in handling API requests which is faster in performance and easy to develop, also it helping us to eliminate the boilerplate code in development so start integrating Retrofit on Kotlin today.

 

Want to create a successful mobile app for your business? Make a wise decision when choosing a developer for your app. Call our experts for collaboration right now.

  

 

The following two tabs change content below.

Sankar Raman

A full stack Android app developer having 4 years of experience in Mobile app development. Well amalgamated Co-worker who rapidly commutes in developing, implementing and adopting new technologies to wider possibilities of App development.

2 thoughts on “How To Use Retrofit On android With Kotlin”

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call