3

I create an application that sends some data to a secured network.

At the server side they need the data as JSON object. For that am creating the data as JSON object and writing that data in the OutputStream of the connection.

But the response from the server side telling it is not getting the data that I am passing.

The code snippet that am using is something like given below:

HttpsConnection _connection = (HttpsConnection)Connector.open("https://gmail.com/",Connector.READ_WRITE, true);             _connection.setRequestMethod(HttpsConnection.POST);
_connection.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT");
_connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
_connection.setRequestProperty("Content-Language", "en-US");
_connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
byte[] postData = jsonObject.toString().getBytes("UTF-8");
_connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
_connection.setRequestProperty("jsondata",jsonObject.toString());
OutputStream os = _connection.openOutputStream();
os.write(postData);
os.flush();

Please help me to solve the issue.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
TKV
  • 2,533
  • 11
  • 43
  • 56

3 Answers3

3

I guess the reason is "Content-Type" => "application/x-www-form-urlencoded". This type of a POST exists for sending a list of key=value pairs. So the server on its side will parse the post data in terms of key=value pairs. I believe in your case it just fails to parse the got post data, because you don't send the data in the key=value pairs form (instead you just pour the entire json string jsonObject.toString().getBytes("UTF-8") in it).

So basically you need to form a key value pair "json=YOUR_JSON_HERE". Then on the server you will get your data as the json parameter value:

URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
encPostData.append("json", jsonObject.toString());
byte[] postData = encPostData.toString().getBytes("UTF-8");

Another option (and BTW it would be the most proper way to do this particular task) would be to use "multipart/form-data" POST type. However it will be a bit harder to implement it if you've never done that before on BB.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
  • thanks for your valuable response..i tried your solution.. but unfortunately still i am not getting the expected output.. the problem here is actually i dn't have any idea about the data accepting and parsing method at the server side..only think i know is the data must be in the json format..like {"header":{"clientName":"clientName","clientVersion":"clientVersion"},"data":{//some json data array}..and the think is am sending in this format itself.. – TKV Apr 05 '11 at 12:03
  • @Tijo K Varghese: It sounds like you are not sure what exactly the server expects from you (e.g. there is no specification on this). And in this case how this task can be done at all? Are you going to keep guessing? – Vit Khudenko Apr 05 '11 at 12:52
  • You are wright..I had sent them to give the proper document for sending data to the server side..anyway thanks for your response.. – TKV Apr 06 '11 at 05:31
  • @Tijo K Varghese: Yes, this is a right way - to ask for a spec first. :) Any way, if you find my answer helpful, then please upvote it (later, when your StackOverflow reputation allows to). – Vit Khudenko Apr 06 '11 at 07:21
1

You have to append appropriate suffix to to your url eg: If you use simulator use:https://gmail.com/;deviceside=true etc

Jisson
  • 3,566
  • 8
  • 38
  • 71
  • @jisson thanks for your answer..i tried the solution you give..but it doesn't give the answer to my question..my question is that am sending the json data with the request...but at the server side the data si not getting..why this is happening..??is there any other way to send data to a secured connection than from writing data to the connection objects outputstream..???[link](http://stackoverflow.com/questions/5549618/blackberry-java-https-connection-am-passing-the-requested-data-but-at-the-serv) – TKV Apr 05 '11 at 10:44
  • http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is_-_Different_ways_to_make_an_HTTP_or_socket_connection.html?nodeid=826935&vernum=0 – Jisson Apr 05 '11 at 11:48
  • @jisson: thanks..now i got the link..but that link is not giving any idea about how can i set data to an httpsConnection..anyway thanks for sending the link.. – TKV Apr 05 '11 at 12:29
  • What type of Server you are using. – Jisson Apr 05 '11 at 13:24
0

I have same this problem but finally find solution:

HttpConnection c = (HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty(
    HttpProtocolConstants.HEADER_CONTENT_TYPE, PostData.getContentType());
c.setRequestProperty(
    HttpProtocolConstants.HEADER_CONTENT_LENGTH,String.valueOf(oPostData.size()));
c.setRequestProperty("Content-Length", Integer.toString(oPostData.size()));
c.setRequestProperty("Content-Type","application/json");
byte [] postDataBytes = jobj.toString().getBytes("UTF-8");
os = c.openOutputStream();
os.write(postDataBytes);
os.flush();      
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95