I want to send the following JSON text
{"Email":"aaa@tbbb.com","Password":"123456"}
to a web service and read the response. I know to how to read JSON. The problem is that the above JSON object must be sent in a variable name jason
.
How can I do this from android? What are the steps such as creating request object, setting content headers, etc.
HttpPost
is deprecated by Android Api Level 22. So, UseHttpUrlConnection
for further.Nothing could be simple than this. Use OkHttpLibrary
Create your json
and send it like this.
Sending a json object from Android is easy if you use Apache HTTP Client. Here's a code sample on how to do it. You should create a new thread for network activities so as not to lock up the UI thread.
You could also use Google Gson to send and retrieve JSON.
Now since the
HttpClient
is deprecated the current working code is to use theHttpUrlConnection
to create the connection and write the and read from the connection. But I preferred to use the Volley. This library is from android AOSP. I found very easy to use to makeJsonObjectRequest
orJsonArrayRequest