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
Developer
misty83
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000
Accepted Solution

how to use post method?

hi,

i need to send an image to the server using post method.

 

i connect using Http, like this:

 

HttpConnection _http = (HttpConnection)Connector.open(address);

 

can anyone provide an example of how can i use a post method to send the data to the server?

 

 kind regards

Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to use post method?

A quick search using the words HTTP and POST on this forum found this:

 

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=8112&query.id=280...

 

I'm sure you will find other samples too.

Please use plain text.
Developer
misty83
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: how to use post method?

but the examples i've found does not work. p.ex:

HttpConnection _http = (HttpConnection) Connector.open(_url); _http.setRequestMethod("POST"); URLEncodedPostData post = new URLEncodedPostData(_url, false); post.append("", "misty"); _http.openOutputStream().write(post.getBytes()); // send the request _http.getResponseCode();

 

and i cannot receive 'misty' at the server. seems as nothing was sent..

Please use plain text.
Developer
misty83
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: how to use post method?

ok, i corrected the code a little:

Bitmap _bitmap = new Bitmap(Display.getWidth(), Display.getHeight()); Display.screenshot(_bitmap); JPEGEncodedImage _jpg = JPEGEncodedImage.encode(_bitmap, 20); _http = (HttpConnection)Connector.open(_url, Connector.READ_WRITE); _http.setRequestMethod("POST"); URLEncodedPostData post = new URLEncodedPostData(_url, false); post.append("param1", ""+_jpg); _http.openOutputStream().write(post.getBytes()); _http.getResponseCode();

 

 

but when i check under the url where an image should appear i see only:

param1=net.rim.device.api.system.JPEGEncodedImage@8caf475
why is that? why i cannot see the image?
Please use plain text.
Developer
misty83
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: how to use post method?

i've solved the problem. here is my code, which works:

 

Bitmap _bitmap = new Bitmap(Display.getWidth(), Display.getHeight()); Display.screenshot(_bitmap); JPEGEncodedImage _jpg = JPEGEncodedImage.encode(_bitmap, 20); DataBuffer _db = new DataBuffer(); _db.write(_jpg.getData()); _http = (HttpConnection) Connector.open(_url, Connector.READ_WRITE); _http.setRequestMethod("POST"); _http.openOutputStream().write(_db.toArray()); _http.getResponseCode();

 

kind regards :smileyhappy:

Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to use post method?

[ Edited ]

I suspect

post.append("param1", ""+_jpg);

will call the toString method on the _jpg object.

 

I think you want to code something like

 

post.append("param1", "");

_http.openOutputStream().write(post.getBytes());

_http.openOutputStream().write(_jpg.getData());

 

Edit: Was in edit, did not realise you had solved it.

Message Edited by peter_strange on 05-01-2009 11:11 AM
Please use plain text.
Developer
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: how to use post method?

You are using URLEncodedPostData.

If you want to send paramateres and files, don't you need to use something that will provide you multipart/form-data encoding?

 

I am not sure if there is a library for that in the RIM SDK.

 

Please use plain text.
New Developer
redbutt
Posts: 37
Registered: ‎03-02-2009
My Device: Bold 9700
My Carrier: TMO

Re: how to use post method?

There is support for multi-part.  Just create a POST request as you would normally, but ensure your POST body has the correct part boundaries defined and content populated, and set the Content-Type header accordingly.

Please use plain text.
Developer
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: how to use post method?

You can anything you want if you like to do it manually. But there does not seem to be a builtin library doing what you described.

Please use plain text.