Code sample: Requesting data using a NonBlockingSenderDestination object

import net.rim.device.api.io.URI;
import net.rim.device.api.io.messaging.*;
import net.rim.device.api.ui.*
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.system.Application;
import java.io.*;

public class NetworkSample extends UiApplication 
{
    public static void main(String[] args) 
    {
        NetworkSample app = new NetworkSample();
        app.enterEventDispatcher();
    }
    public NetworkSample() 
    {
        pushScreen(new NonBlockingSenderSample());
    }
}

class NonBlockingSenderSample extends MainScreen 
                                                 implements FieldChangeListener
{

    ButtonField _btnNonBlock = new ButtonField(Field.FIELD_HCENTER);

    private static UiApplication _app = UiApplication.getUiApplication();

    public NonBlockingSenderSample()
    {
        _btnNonBlock.setChangeListener(this);
        _btnNonBlock.setLabel("Fetch page");

        add(_btnNonBlock);
    }

    public void fieldChanged(Field button, int unused)
    {

        if(button == _btnNonBlock)
        {
            NonBlockingSenderDestination destination = null;
            try
            {
                URI uri = URI.create("http://www.blackberry.com");

                NBSDMsgListener responseListener = new NBSDMsgListener();
                destination = (NonBlockingSenderDestination)
                                  DestinationFactory.getSenderDestination
                                      ("CommAPISample", uri);
                if (destination == null)
                {
                    destination =
                        DestinationFactory.createNonBlockingSenderDestination
                            (new Context("CommAPISample"), uri, responseListener);
                }

                // Send message to retrieve the response
                destination.send();

            }


            catch(Exception e)
            {
                // process the error
            }

        }
    }

}

class NBSDMsgListener implements MessageListener 
{

    public void onMessage(Destination dest, Message msg) 
    {

        String payload = null;

        if (msg instanceof ByteMessage)
        {
            ByteMessage reply = (ByteMessage) msg;
            payload = (String) reply.getStringPayload();
        } else if(msg instanceof StreamMessage)
        {
            StreamMessage reply = (StreamMessage) msg;
            InputStream is = reply.getStreamPayload();
            byte[] data = null;
            try {
                data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
            } catch (IOException e) {
            }
            if(data != null)
            {
                payload = new String(data);
            }
        }
        if(payload!=null)
        {
            synchronized(Application.getEventLock())
            {
                UiApplication.getUiApplication().pushScreen
                                                (new HTTPOutputScreen(payload));
            }


        }
    }

    public void onMessageCancelled(Destination arg0, int arg1) 
    {
        // process message cancelled notification
    }

    public void onMessageFailed(Destination arg0, MessageFailureException arg1) 
    {
        // process message failed notification
    }

}

class HTTPOutputScreen extends MainScreen 
{

    RichTextField _rtfOutput = new RichTextField();

    public HTTPOutputScreen(String message)
    {
        _rtfOutput.setText("Retrieving data. Please wait...");
        add(_rtfOutput);
        showContents(message);
    }

    // After the data has been retrieved, display it
    public void showContents(final String result) {
        UiApplication.getUiApplication().invokeLater(new Runnable() 
        {

            public void run() 
            {
                _rtfOutput.setText(result);
            }
        });
    }
}

Was this information helpful? Send us your comments.