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

New Contributor
Posts: 2
Registered: ‎09-07-2010
My Device: Not Specified
Accepted Solution

How to POST JSON??

[ Edited ]

I'm new to Blackberry development and I have been unable to get a POST to work.  I reviewed the other related posts on the forum but they have not helped.  (example: http://supportforums.blackberry.com/t5/Java-Development/JSON-RPC-How-to-POST-from-blackberry/m-p/478...)

Here is the code:



		ConnectionFactory connFactory = null;
		ConnectionDescriptor connDescriptor = null;
		HttpConnection httpConn = null;
		OutputStream stream = null;
		InputStream inputStream = null;
		String foo = "{\"first\":\"joe\",\"last\":\"smith\"}";
		try {			
			connFactory = new ConnectionFactory();
			connDescriptor = connFactory.getConnection(url);
			if (connDescriptor != null) {
				httpConn = (HttpConnection)connDescriptor.getConnection();
				httpConn.setRequestProperty("Content-Language", "en-US");
				byte[] postdata = foo.getBytes("UTF-8");				
				httpConn.setRequestProperty("Content-Length", Integer.toString(postdata.length));

				stream = httpConn.openOutputStream();
				int responseCode = httpConn.getResponseCode();

				StringBuffer sb = new StringBuffer();
				inputStream = httpConn.openInputStream();
				InputStreamReader isReader = new InputStreamReader(inputStream);
				char i;
				int j = 0;
				while( (j = isReader.read()) != -1) {
					i = (char)j;

		} catch (Exception e) {



Server side code:



String query = null;
String json = null;

json = Request.Form["add"];
JavaScriptSerializer js = new JavaScriptSerializer();
MyObject o =  js.Deserialize<MyObject>(json);


Posting from my Android app is successful.  What am I missing???




Posts: 15
Registered: ‎08-26-2010
My Device: None
My Carrier: None

Re: How to POST JSON??

If you look at my post




It looks like you need to use URLEncodedPostData as you are using form variables, in your case 'add' on the server side. In my code it's 'Data'.


However as you can tell from my post i'm having issues when to trying to post more than 1K with certain devices 8900 for example. I'm currently investigating to see if this because i'm using form variables

Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: How to POST JSON??

In your BB code, close outputStream before reading the response code (or at least flush it).


Also (although not part of getting the POST to work), you should close both the input stream from the connection and the connection itself after you are through with them. To avoid memory leaks, you should close these in a finally block added to the try/catch:


try {
} catch (Exception e) {
} finally {
if (inputStream != null) {
try { inputStream.close(); }
catch (Exception ignored) {}
if (httpConn != null) {
try { httpConn.close(); }
catch (Exception ignored) {}

Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.