{"id":2194,"date":"2020-03-06T21:13:47","date_gmt":"2020-03-06T11:13:47","guid":{"rendered":"https:\/\/learntodroid.com\/?p=2194"},"modified":"2021-01-13T09:54:36","modified_gmt":"2021-01-12T23:54:36","slug":"how-to-send-json-data-in-a-post-request-in-android","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/how-to-send-json-data-in-a-post-request-in-android\/","title":{"rendered":"How to Send JSON Data in a POST Request in Android"},"content":{"rendered":"\n
Retrofit2 is a powerful HTTP client library developed by Square used in Android and Java development. I have put together a tutorial with code samples in Java covering how to send JSON data in a HTTP POST request within an Android app using Retrofit2. <\/p>\n\n\n\n
To send JSON data in a POST request in Android using Retrofit2 you need to complete the following steps.<\/p>\n\n\n\n
Continue reading this post to deep dive into what Retrofit2 is and how it can be used to generate a POST request containing JSON data with code samples available in this article in Java.<\/p>\n\n\n\n
I have also added some information to the tutorial around an alternative approach for sending data in a POST request with Retrofit2 by using URL parameters instead of a JSON body inside the request.<\/p>\n\n\n\n
We will be creating a very basic Android app for this tutorial to demonstrate how to make a HTTP POST request containing a JSON body using Retrofit2.<\/p>\n\n\n\n
Please see a screenshot of the Android app will be creating. The app contains a basic form that is used to capture a comment which is like something that you might see on a social media app or a news app with articles the public can comment on.<\/p>\n\n\n\n
As you can see it contains the following:<\/p>\n\n\n\n
I have uploaded the code for the Android app being created in this tutorial in GitHub – https:\/\/github.com\/learntodroid\/PostRequestwithJSON<\/a><\/p>\n\n\n\n For the purposes of this tutorial I have written code for a very basic API using Python and the library Flask which is configured to respond with the same Comment sent to the API in POST request from the Android app.<\/p>\n\n\n\n If you are interested in the code for the API written in Python using the Flask library it is also available on GitHub – https:\/\/github.com\/learntodroid\/PostRequestFlaskApi<\/a><\/p>\n\n\n\n If you want to run this API make sure you have Python and PyPi installed on your local machine where you are running the Android emulator and follow the brief instructions in the Readme to retrieve the dependencies for Flask and run the API.<\/p>\n\n\n\n In this tutorial we will be making use of various 3rd party libraries to simplify the effort required to integrate our Android app with an API to make a POST request.<\/p>\n\n\n\n The 3rd party libraries we will use include Retrofit2, OkHttp3 and GSON. Each of the libraries will be retrieved into our Android project using Gradle.<\/p>\n\n\n\n Retrofit2 is a HTTP client for Android and Java developed by Square which allows you to use a Java interface to simplify the process of integrating with a HTTP API.<\/p>\n\n\n\n To retrieve the Retrofit2 library using Gradle include the following implementation dependency inside your app’s build.gradle file (see a complete build.gradle file at the end of this section which covers all required dependencies). <\/p>\n\n\n\n Retrofit2 requires Java 8, to include Java 8 in an Android project, update your app’s build.gradle file to include compile options.<\/p>\n\n\n\n OkHttp3 is a HTTP client for Java and Kotlin developed by Square.<\/p>\n\n\n\n For the purposes of this tutorial we will be using the logging interceptor included in the OkHttp3 library to give us visibility of the HTTP request and response logs for our Android app within Logcat. <\/p>\n\n\n\n To get an idea of what we will be able to see in Logcat using the OkHttp3 logging interceptor, have a look at the screenshot below<\/p>\n\n\n\n To retrieve the logging interceptor from the OkHttp3 library using Gradle include the following implementation dependency inside your app’s build.gradle file.<\/p>\n\n\n\n Gson is a Java library by Google that supports converting objects in Java into JSON format and back otherwise known as serialization and deserialization.<\/p>\n\n\n\nStep 1: Obtaining the Retrofit2 Dependencies<\/h3>\n\n\n\n
Retrofit2<\/h4>\n\n\n\n
implementation 'com.squareup.retrofit2:retrofit:2.7.1'<\/strong><\/pre>\n\n\n\n
android {\n compileOptions {\n sourceCompatibility JavaVersion.VERSION_1_8<\/strong>\n targetCompatibility JavaVersion.VERSION_1_8<\/strong>\n }\n} <\/pre>\n\n\n\n
OkHttp3<\/h4>\n\n\n\n
<\/figure><\/div>\n\n\n\n
dependencies {\n <\/strong>implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'\n<\/strong>}<\/pre>\n\n\n\n
GSON<\/h4>\n\n\n\n