Request data using a BlockingSenderDestination object
-
Import the required classes and interfaces.
import net.rim.device.api.io.messaging.*;
import net.rim.device.api.io.URI;
-
Create a thread from which to call
BlockingSenderDestination.sendReceive().
Thread t = new Thread( new Runnable()
{
public void run()
{
-
Create a
Message object to hold the response from the URL.
Message response = null;
-
Create a
URI object to pass to the
DestinationFactory class.
URI uri = new URI("http://www.blackberry.com");
-
Create a
BlockingSenderDestination object.
BlockingSenderDestination bsd = null;
-
Retrieve the
BlockingSenderDestination object for your context,
if one exists.
try
{
bsd = (BlockingSenderDestination)
DestinationFactory.getSenderDestination
("MyContext", uri);
-
If no
BlockingSenderDestination exists, create one.
if(bsd == null)
{
bsd = DestinationFactory.createBlockingSenderDestination
(new Context("MyContext"), uri);
}
-
Send a message and wait for the response.
response = bsd.sendReceive();
-
If the web service sent a response, process the response.
if(response != null)
{
processResponse(response);
}
}
-
Catch any errors that occur if the message could not be sent for
some reason.
catch (Exception e)
{
// Process the error
}
-
Release the
BlockingSenderDestination.
finally
{
if(bsd != null)
{
bsd.release();
}
}
}
});
-
Start the thread.
t.start();
After you finish:
If your
processResponse() updates the UI, you must do so on the
main event thread, not on the thread that you created in this task. For more
information, read the knowledge base article at
http://supportforums.blackberry.com/t5/Java-Development/Manage-UI-interactions/ta-p/502378.
For a complete code sample, see "Code sample:
Requesting data using a BlockingSenderDestination object".
Was this information helpful? Send us your comments.