How to use Retrofit to get data from stackoverflow API and GitHub Demo in android studio

Retrofit is a type-safe REST client for Android developed by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp. See this guide to understand how OkHttp works.

This library makes downloading JSON or XML data from a web API fairly straightforward. Once the data is downloaded then it is parsed into a Plain Old Java Object (POJO) which must be defined for each "resource" in the response.

Setup
Must be defined on internet permission on AndroidManifest.xml .


Dependencies
Add following dependencies on app/build.gradle file:



Converter:
Here is the different type of  Gson converter, we can use them:



How to create retrofit instance, 
then we have to define, the userendpoint:
Now, create model class, user variables, In MainActivity.java, we have to create instance of APIClient and call the users.

Download Full code: https://github.com/dharmakshetri/Android-API-Demos

More details:
https://square.github.io/retrofit/
https://github.com/square/retrofit
https://guides.codepath.com/android/Consuming-APIs-with-Retrofit
https://realm.io/news/droidcon-jake-wharton-simple-http-retrofit-2/
http://www.vogella.com/tutorials/Retrofit/article.html

Happy Coding !!!

No comments:

Post a Comment

Circle

Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace