In this blog, we're going to learn how can we get the data from API services to our android apps using RetroFit Library.
RetroFit is the third-party library used to get data from the internet. Volley Library also goes in the same stream. But, mostly we choose Retro Fit as the best option. Using RetroFit is quite simple and straightforward.
Start by adding an entry in your project's build. gradle file.
Then click sync now. This will add Retro-Fit to your Android Project.
Else you can download .jar files and place it in the lib folder ( But if you aren't a lot familiar with Android Studio IDE then Gradle integration is RECOMMENDED)
Then you need to define Interfaces that will be used by Retrofit to make calls to your REST API endpoints.
Take a look at an example below to understand better
Now if you have done defining the API Interfaces. You can just have a test run for your app.
For starting you need to create an instance of RESTAdapter and set your base URL to API's backend.
That's all. Simple as that.
And now RetroFit will read your information provided in the interface and under the hood, it will create Rest Handler according to the meta info given by you and makes an HTTP request.
Once the response is received. If it is in JSON format then you have to use Gson library to prase it to show up organised data that you fetched from the web.
That's all for this, the blog on JSON parsing will be soon available. Stay tuned by subscribing to the blog.
Use the comments section to ask me about any issues.
RetroFit is the third-party library used to get data from the internet. Volley Library also goes in the same stream. But, mostly we choose Retro Fit as the best option. Using RetroFit is quite simple and straightforward.
Start by adding an entry in your project's build. gradle file.
compile 'com.squareup.retrofit:retrofit:1.7.1' |
Then click sync now. This will add Retro-Fit to your Android Project.
Else you can download .jar files and place it in the lib folder ( But if you aren't a lot familiar with Android Studio IDE then Gradle integration is RECOMMENDED)
Then you need to define Interfaces that will be used by Retrofit to make calls to your REST API endpoints.
Take a look at an example below to understand better
public interface Api {
//You can use rx.java for sophisticated composition of requests
@GET("/users/{user}")
public Observable<SomeUserModel> fetchUser(@Path("user") String user);
//or you can just get your model if you use json api
@GET("/users/{user}")
public SomeUserModel fetchUser(@Path("user") String user);
//or if there are some special cases you can process your response manually
@GET("/users/{user}")
public Response fetchUser(@Path("user") String user);
}
Now if you have done defining the API Interfaces. You can just have a test run for your app.
For starting you need to create an instance of RESTAdapter and set your base URL to API's backend.
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://yourserveraddress.com")
.build();
YourUsersApi yourUsersApi = restAdapter.create(Api.class);
That's all. Simple as that.
And now RetroFit will read your information provided in the interface and under the hood, it will create Rest Handler according to the meta info given by you and makes an HTTP request.
Once the response is received. If it is in JSON format then you have to use Gson library to prase it to show up organised data that you fetched from the web.
That's all for this, the blog on JSON parsing will be soon available. Stay tuned by subscribing to the blog.
Use the comments section to ask me about any issues.
0 Comments