02-17-2013 05:49 PM
Right now, I have a webserver with some information on it. To be as transparent as possible lets say that the webserver has information about John. So when I send an http post request about John the server should check and see whether or not the information I provided is correct. My problem is that I am definitely writing my HttpPost request wrong. I have tried a few sites but I can't seem to get a firm hold on how exactly to write an HttpPost. I want to send my JSONHashtable and compare it to the data the server has but I am lost at the moment. But here is how I am approaching the problem.
"age" = "26"
"occupation" = "CEO"
(This is Pseudo code for testing if the server will send me an error if I send this data)
JSONObject about = new JSONObject();
JSONHashtable john = new JSONHashtable(about); //I have this table but how should I send it to the server?
HttpConnection request = (HttpConnection) Connector.open("server name");
//Do I need to write an if statement here? Saying compare my table with server values and send me an error if it cannot?
Any help would be greatly appreciated. Thank you all in advance!
02-18-2013 04:28 AM
The problem you are having sending an http post is understandable. All the documentation expects that you know how to send one and what to send. I have not yet found a good example that also teaches Http. And that is the problem, there is background information required in http that is not related to Java or BlackBerry.
The best I can say is that you should research http POST, using places like Wikipedia, or even searching on this forum, other people have asked similar questions. It would also be useful to have a 'friend' on the Web Server side of things so that you know what to send because you ask this person.
There is an example that uses a POST in the API documentation - search the examples supplied with HttpConnection.
In my experience, the important headers to set are the content length and the content type. But whether these are required will depend on your Server.
02-18-2013 09:14 AM
Hello Peter and RinceThomas33,
Thank you for your response. Will using a connection factory/connection descriptor help me hit the server? I think my issue is not that I am not sending any data but rather my endpoint might be really off. If this is not the problem then I think my problem would be the url and that the server name "localhost:xxxxx" might be an url bb wouldn't understand. If this is the case how would you propose I solve that problem?
02-18-2013 09:23 AM
BB Simulator and device will NOT understand localhost, use the IP Address instead.
ConnectionFactory helps when you are choosing which of the multiple paths to the Server you need to use. From a SImulator you can always use
as the connection suffix so just use that until you get things going. When you get things going, you might think about changing to use ConnectionFactory, I would recommend it.
When you attempt a connection, and it fails, there is usually some detail text associated wit the Exception thrown - it will help you figure out what is going wrong.
Another options is to start with httpDemo sample, get that working (which does require the MDS Simulator from memory) and then change the request to a POST.
02-20-2013 09:41 AM
Sorry for the late response. deviceside=true helped a lot. What was wrong was that I had the wrong url and I also changed the server name via going into etc/hosts and changing the localhost name to something else. That seemed to help. Also I had to import the wifi connection thing where I used "interface=wifi;" at the end of "device=true". Unfortunately I have another problem which is that it seems that my json objects aren't actually sending.
02-20-2013 10:04 AM
Don't code deviceside=true and interface=wifi, just one or the other.
You should not need interface=wifi on the Simulator. In fact it can get in the way, but that depends on the Simulator.
Your JSON should be packaged as bytes and written to the output stream associated with he http connection before you request a response. The API documentation POST example I pointed you at earlier shows you enough I think for you get to grips with that. You will need to ask your Server person what content type your should set in the Header, and whether the length is important.