Retrofit 2 for Beginners — Creating an Android API Client

Retrofit is a REST Client for Android and Java. It makes it relatively easy to retrieve and JSON (or other structured data). Retrofit uses the OkHttp library for HTTP requests.

So in this tutorial, I will create a simple app to get json data from server and display that into recyclerview.

our simple application

Content

  • Creating new project
  • Model
  • Setup Retrofit
  • Creating UI and Adapter
  • Making a request
  • Conclusion

Creating new project

Note: In this project, I will use https://ghibliapi.herokuapp.com/films to get Movies data of Ghibli.

Ok, let’s coding by create new android project (java) with 3 packages: adapter, model, webservice .

Add Retrofit, Gson OkHTTP into dependencies (build.gradle file)

  • Retrofit: Type-safe HTTP client for Android
  • GSON: A library to convert Java Objects into JSON and back

We use OkHTTP for logging.

// GSON and retrofit    
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
// Log for retrofit
compile 'com.squareup.okhttp3:logging-interceptor:3.9.1'
compile 'com.squareup.okhttp3:okhttp:3.9.1'
compile 'com.android.support:recyclerview-v7:26+'

Because we are working with network so we have to add INTERNETpermission into AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET"/>

Model

Let create Movie.java file under model folder. For easy, we just get title, description, director of movie.

Setup Retrofit

Create a file named ApiInterface where we can put all URI in this file

And now create ServiceGenerator file for Retrofit configuration.

Creating UI and Adapter

At activity_main.xml, add RecyclerView UI:

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Create item_movie.xml file, this is a item of our recyclerview, just display title , description , director of a movie.

And the last thing is about Adapter . Let’s create a file named MoviesAdapter under adapter folder.

Making a request

And the last thing is create a request in MainActivity.java

Let’s run to see what will happen!

Conclusion

It’s very simple application to introduce Retrofit. I have used Volley but For me,Retrofit is the best library with network operations because of its simplicity. In Retrofit we configure which converter is used for the data serialization. In this case, we use GSON .

Hope this article is helpful for you!

You can find full code in this github link.

You are beginner Android Developer and want to “upgrade” your Android Skill by just 10$, please check out my Udemy course. Refund within 30 days if you dont’t like it.

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store