I’m trying to figure out how to POST JSON from Android by using HTTPClient. I’ve been trying to figure this out for a while, I have found plenty of examples online, but I cannot get any of them to work. I believe this is because of my lack of JSON/networking knowledge in general. I know there are plenty of examples out there but could someone point me to an actual tutorial? I’m looking for a step by step process with code and explanation of why you do each step, or of what that step does. It doesn’t need to be a complicated, simple will suffice.
Again, I know there are a ton of examples out there, I’m just really looking for an example with an explanation of what exactly is happening and why it is doing that way.
If someone knows about a good Android book on this, then please let me know.
Thanks again for the help @terrance, here is the code I described below
public void shNameVerParams() throws Exception{
String path = //removed
HashMap params = new HashMap();
params.put(new String("Name"), "Value");
params.put(new String("Name"), "Value");
try {
HttpClient.SendHttpPost(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In this answer I am using an example posted by Justin Grammens.
About JSON
JSON stands for JavaScript Object Notation. In JavaScript properties can be referenced both like this
object1.nameand like thisobject['name'];. The example from the article uses this bit of JSON.The Parts
A fan object with email as a key and foo@bar.com as a value
So the object equivalent would be
fan.email;orfan['email'];. Both would have the same valueof
'foo@bar.com'.About HttpClient Request
The following is what our author used to make a HttpClient Request. I do not claim to be an expert at all this so if anyone has a better way to word some of the terminology feel free.
Map
If you are not familiar with the
Mapdata structure please take a look at the Java Map reference. In short, a map is similar to a dictionary or a hash.Please feel free to comment on any questions that arise about this post or if I have not made something clear or if I have not touched on something that your still confused about… etc whatever pops in your head really.
(I will take down if Justin Grammens does not approve. But if not then thanks Justin for being cool about it.)
Update
I just happend to get a comment about how to use the code and realized that there was a mistake in the return type.
The method signature was set to return a string but in this case it wasnt returning anything. I changed the signature
to HttpResponse and will refer you to this link on Getting Response Body of HttpResponse
the path variable is the url and I updated to fix a mistake in the code.