Retrofit: Another Powerful and simple http client for android

CodingWithTashi
3 min readFeb 27, 2021

Fun fact: Android 3.0 (Honeycomb) Is The Only Version To Never Run On Phone

In this post we will see how can we implement Retrofit in your application

If you prefer to read official doc rather then get annoy by me go read this. But make sure you read it.I am watching you 😊

If you are sticking with me than lets so this. first thing first

  1. What is retrofit?
  • If you heard about volley, okhttp or any other http request client then retrofit is alternative to them.
  • If you are hearing this for the firstime then you are late to party well, We are good people and you are still welcome
    Joke aside
  • Retrofit is an awesome type-safe HTTP client for Android and Java built by awesome folks at Square.
  • Retrofit makes it easy to consume JSON or XML data which is parsed into Model class

2.Why retrofit when we have volley,okhttp etc ?

Well, Is has

  • Excellent API documentation
  • Good support in communities
  • Easy to add into your projects

Get the latest dependency from here
https://github.com/square/retrofit

3. An example with retrofit

Content :

  1. Add depedency
  2. Add Internet Permission
  3. Get dummmy photo json data
  4. Create pojo class for Photo
  5. Create Retrofit Instance class
  6. Define Service class to perform get,delete,put etc
  7. Create Photo Adapter to consumr photo list
  8. Finally in MainActivity.java call API
  9. TL;DR
  10. Add retrofit dependency in your build.gradle file. if you are using image in your app. Then add glide library too.

2. Add internet permission in AndroidManifest.xml to access internet

3. we will use jsonplaceholder to get dummy url as below

4. Create pojo/model class with field albumId,id,title,thumbnailUrl

5. Create Retrofit Instance class

6. Define Service class ApiService.java to perform get,delete,put etc

7. Create PhotoAdapter.java to consumr photo list and display in recycler view

before implement adapter create new xml for adapter and named it single_photo_item.xml

Now in PhotoAdapter.java

8. Finally in MainActivity.java and activity_main.xml call API

In activity_main.xml

In MainActivity.java

In getPhotoListFromServer function, we will implement logic of retrofit to get the photo data from server.

After adding function you code should look like below

Huu! That all, run the app and you should able to get list of blog in your app.

go take yourself a break, you earned it.

9. TL;DR

  • Retrofit is a REST Client for Java and Android.
  • It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice.
  • In Retrofit you configure which converter is used for the data serialization.

That all, Thanks guys, I hope you like this blog. please make sure to give clap 👏 and leave some engagement.

--

--