Android Retrofit: POST
Consuming method POST
Retrofit is a very powerful library for making requests using HTTP verbs and that can be configured using Java annotations.
In this example we will explore the post method, first add following dependences in the grade file.
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'implementation 'com.squareup.retrofit2:retrofit:2.4.0'
Add the internet permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
JSONPlaceholder is a free online REST API that you can use whenever you need some fake data.
https://jsonplaceholder.typicode.com/posts
we will use this example API
Add model Posts.java
We must create a interface it will help us to manage each of our API requests, in this we will have the following code
Last step is create activity, in this example only will use a simple Textview to show data. In MainActivity.java
Response if is success
{ id: 101, title: 'foo', body: 'bar', userId: 1 }
The next post touch same example with Kotlin, you can download the repo :D
Tnks.