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.
Android doesn't have special code for sending and receiving HTTP, you can use standard Java code. I'd recommend using the Apache HTTP client, which comes with Android. Here's a snippet of code I used to send an HTTP POST.
I don't understand what sending the object in a variable named "jason" has to do with anything. If you're not sure what exactly the server wants, consider writing a test program to send various strings to the server until you know what format it needs to be in.
There's a surprisingly nice library for Android HTTP available at the link below:
http://loopj.com/android-async-http/
Simple requests are very easy:
To send JSON (credit to `voidberg' at https://github.com/loopj/android-async-http/issues/125):
It's all asynchronous, works well with Android and safe to call from your UI thread. The responseHandler will run on the same thread you created it from (typically, your UI thread). It even has a built-in resonseHandler for JSON, but I prefer to use google gson.