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
Highlighted
New Contributor
Posts: 5
Registered: ‎12-19-2011
My Device: 9860
My Carrier: A1

Http-Method PUT not working with FireAndForgetDestination

I'm trying to send a json object from my bb to a REST service. As I can see, that the request arrives on the server, but not with the Request method PUT but with method GET. Hence the desired method in my REST service won't be executed. Here is my code: FireAndForgetDestination ffd = null; try { String uriStr = appModel.getBmsUri() + "/eintraege"; ffd = (FireAndForgetDestination) DestinationFactory .getSenderDestination("BmsEintraegeContext", URI.create(uriStr)); if (ffd == null) { ffd = DestinationFactory.createFireAndForgetDestination( new Context("BmsEintraegeContext"), URI.create(uriStr)); } ByteMessage bm = ffd.createByteMessage(); bm.setStringPayload(json.toString()); bm.setTransportHeader("Content-Type", "application/json"); // cast ByteMessage to HttpMessage and set Request method to PUT ((HttpMessage)bm).setMethod(HttpMessage.PUT); ffd.sendNoResponse(bm); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } finally { if (ffd != null) { ffd.release(); } } Any idea what's wrong here? Thanks for help. Regards, Walter
Developer
Posts: 52
Registered: ‎11-24-2011
My Device: Bold 9930
My Carrier: Rogers

Re: Http-Method PUT not working with FireAndForgetDestination

There is 'Insert Code' button to display the code in a human readable format.

New Contributor
Posts: 5
Registered: ‎12-19-2011
My Device: 9860
My Carrier: A1

Re: Http-Method PUT not working with FireAndForgetDestination

sorry for the unreadable posting, but unfortunately I can't see this button.
I've posted the message from my browser and the userinterface of the website isn't that charming...
Or is there another UI I should know about?

So I try to post the code once again from my Browser:

FireAndForgetDestination ffd = null;

try {
String uriStr = appModel.getBmsUri() + "/eintraege";

ffd = (FireAndForgetDestination) DestinationFactory
.getSenderDestination("BmsEintraegeContext", URI.create(uriStr));
if (ffd == null) {
ffd = DestinationFactory.createFireAndForgetDestination(
new Context("BmsEintraegeContext"), URI.create(uriStr));
}

ByteMessage bm = ffd.createByteMessage();
bm.setStringPayload(json.toString());
bm.setTransportHeader("Content-Type", "application/json");

// cast ByteMessage to HttpMessage and set Request method to PUT
((HttpMessage)bm).setMethod(HttpMessage.PUT);

ffd.sendNoResponse(bm);
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
finally {
if (ffd != null) {
ffd.release();
}
}
Developer
Posts: 52
Registered: ‎11-24-2011
My Device: Bold 9930
My Carrier: Rogers

Re: Http-Method PUT not working with FireAndForgetDestination

Maybe I'm wrong but the problem is that HttpMessage and ByteMessage are interfaces extended from Message.

I am actually surprised it allowed you to cast one to the other.

Isn't it possible to use setTransportHeader() for setting method?

New Contributor
Posts: 5
Registered: ‎12-19-2011
My Device: 9860
My Carrier: A1

Re: Http-Method PUT not working with FireAndForgetDestination

Using the setTransportHeader("Http-Method", "PUT") method does not work, it's the wrong place.
My REST service receives the request and is logging following lines:

ID: 7
Address: http://localhost:8480/bmsRest/bms/eintraege
Http-Method: GET
Content-Type: application/json
Headers: {Accept=[*/*], connection=[close], Content-Length=[336], content-type=[application/json], host=[localhost:8480], http-method=[PUT], user-agent=[Communication API Library], via=[MDS_4.1.5.26]}
Payload: {"eintrag":{"abteilung":"IP","autor":"PLASRC","prio":"NORMAL","betreff":"Test","beschreibung":"initial entry\rChange1\rChange 2","erstelltAm":"2011-07-20T13:01:02.699+00:00","gelesen":false,"uid":"4e859ded-f04e-4b21-8092-c441343519b3"}}
--------------------------------------

The cast is working, I saw it in the example of the documentation, even if in the example the POST method is used.
http://docs.blackberry.com/en/developers/deliverables/21128/Send_data_using_FireandForgetDestination...

Should it be possible to use Http POST und PUT methods within th bb Communication API (FireAndForgetDestination, Non- BlockingSenderDestination) or do I have to use the Network API?

Regards,
Walter

New Contributor
Posts: 5
Registered: ‎12-19-2011
My Device: 9860
My Carrier: A1

Re: Http-Method PUT not working with FireAndForgetDestination

the cast is working, because Message and HttpMessage both are interfaces. At runtime there will be created an instance of net.rim.device.api.io.messaging.internal.messaging.http.HttpByteMessageImpl which implements both of the interfaces, message and HttpMessage.
I've also tried using setMessageProperty("Http-Method", "PUT") and setTransportHeader(...) but without success, the method used is always the same: GET

Has anybody ever used FireAndForgetDestination with Http-Method POST or PUT?

In the meantime I've created a workaround by using HttpConnection class.
Here is the code of my workaround:

c = (HttpConnection)Connector.open(url);

// Set the request method and headers
c.setRequestMethod("PUT");
c.setRequestProperty("User-Agent",
"Profile/MIDP-2.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");
c.setRequestProperty("Content-Type", "application/json");

// Getting the output stream may flush the headers
os = c.openOutputStream();
os.write(json.toString().getBytes());

rc = c.getResponseCode();


Never the less I would be interested, if posting data (in json or xml format) to a http server should also work with FireAndForgetDestination or BlockingSenderDestination objects.

Cheers,
Walter