How to use Retrofit in Android Studio?

This is a simple tutorial on how to implement Retrofit, the REST api in your android application. If you are intending to save the REST data for offline usage, then How to use GreenDao in android studio? is a good read. Make sure you download the complete project.

Donwload

STEPS

PART 1: Setting up Retrofit REST Data.

In this tutorial, we are considering this json data.

You can host it here: http://myjson.com/

Inorder to format and validate the json, you can use : https://jsonformatter.curiousconcept.com/

 

PART 2 : Setting up Retrofit in Android Studio

  1. Add the dependency in your gradle.
  2. Add Internet Permission in the AndroidManifest.xml

  3. Now we need to create POJO (Plain Old Java Object  or Simple Normal Class ) for our JSON Data.
  4. Go Here: http://www.jsonschema2pojo.org/
  5. Add the json data and enter all the details
  6. Create a new Package in your Android Project and add the downloaded java files into the POJO folder (drag and drop).
    com.codekrypt.retroifit  >  New  > Package
  7. I bileave, you have something like this.2
  8. Create another class called REST.java to handle REST Interface.

PART 3: Setting Custom List View Adapter.

I have create a custom list adapter to handle extra fields. ( In case your application is listing more than one data in the list item. )

So let use see the code for custom List view adapter.

list_item.xml

gradle dependencies.

adapter.xml

PART 4: MainActivity.java (Calling REST)

activity_main.xml  ( we have added listview )

MainActivity.java

That’s it .

When you launch the app, you will be able to see the company names, on the list view.

Reference :
https://www.simplifiedcoding.net/retrofit-android-tutorial-to-get-json-from-server/

https://api.myjson.com/bins/1w1ac

http://stackoverflow.com/questions/25647881/android-asynctask-example-and-explanation

https://blog.robinchutaux.com/blog/a-smart-way-to-use-retrofit/

https://futurestud.io/tutorials/retrofit-getting-started-and-android-client

https://guides.codepath.com/android/Consuming-APIs-with-Retrofit

Tags: