Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
pxlam15
Posts: 25
Registered: ‎02-04-2013
My Device: Torch

Checking information with a Server

Hello all,

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.

Webserver
John
{

   "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();
about.put("age", "24");

about.put("Occupation", "Lumberjack");

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");

request.setRequestMethod(HttpConnection.POST);

 

//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!

Pete

 

 

New Developer
rincethomas33
Posts: 39
Registered: ‎12-31-2010
My Device: 9800

Re: Checking information with a Server

use php webservice to do this. On your php code, check the values are equal or not. Then send corresponding values back to you.

R33
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Checking information with a Server

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. 

Contributor
pxlam15
Posts: 25
Registered: ‎02-04-2013
My Device: Torch

Re: Checking information with a Server

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?

Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Checking information with a Server

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

";deviceside=true"

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. 

Contributor
pxlam15
Posts: 25
Registered: ‎02-04-2013
My Device: Torch

Re: Checking information with a Server

Hey Peter,

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.

Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Checking information with a Server

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.