Request data using a BlockingSenderDestination object

  1. Import the required classes and interfaces.
    import net.rim.device.api.io.messaging.*;
    import net.rim.device.api.io.URI;
  2. Create a thread from which to call BlockingSenderDestination.sendReceive().
    Thread t = new Thread( new Runnable()
    {
        public void run()
        {
  3. Create a Message object to hold the response from the URL.
            Message response = null;
  4. Create a URI object to pass to the DestinationFactory class.
            URI uri = new URI("http://www.blackberry.com");
  5. Create a BlockingSenderDestination object.
            BlockingSenderDestination bsd = null;
  6. Retrieve the BlockingSenderDestination object for your context, if one exists.
            try
            {
                bsd = (BlockingSenderDestination) 
                          DestinationFactory.getSenderDestination
                                                 ("MyContext", uri);
  7. If no BlockingSenderDestination exists, create one.
                if(bsd == null)
                {
                    bsd = DestinationFactory.createBlockingSenderDestination
                                                 (new Context("MyContext"), uri);
                }
  8. Send a message and wait for the response.
                response = bsd.sendReceive();
  9. If the web service sent a response, process the response.
                if(response != null)
                {
                    processResponse(response);
                }
            }
  10. Catch any errors that occur if the message could not be sent for some reason.
            catch (Exception e)
            {
                // Process the error
            }
  11. Release the BlockingSenderDestination.
            finally
            {
                if(bsd != null)
                {
                    bsd.release();
                }
            }
        }
    
    });
  12. 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.