Code samples

Code sample: Requesting data using a BlockingSenderDestination object

import net.rim.device.api.io.messaging.*;
import net.rim.device.api.io.URI;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import java.io.*;

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

class BlockingSenderSample extends MainScreen implements FieldChangeListener
{

    ButtonField _btnBlock = new ButtonField(Field.FIELD_HCENTER);

    private static UiApplication _app = UiApplication.getUiApplication();

    private String _result;

    public BlockingSenderSample()
    {
        _btnBlock.setChangeListener(this);
        _btnBlock.setLabel("Fetch page");

        add(_btnBlock);
    }

    public void fieldChanged(Field button, int unused)
    {

        if(button == _btnBlock)
        {

            Thread t = new Thread(new Runnable() 
            {
                public void run()
                {
                    Message response = null;
                    String uriStr = "http://www.blackberry.com";
                    BlockingSenderDestination bsd = null;
                    try
                    {
                        bsd = (BlockingSenderDestination)
                                   DestinationFactory.getSenderDestination
                                       ("CommAPISample", URI.create(uriStr));
                        if(bsd == null)
                        {
                            bsd =
                              DestinationFactory.createBlockingSenderDestination
                                  (new Context("CommAPISample"),
                                   URI.create(uriStr)
                                   );
                        }

                        // Send message and wait for response
                        response = bsd.sendReceive();

                        if(response != null)
                        {
                            BSDResponse(response);
                        }
                    }
                    catch(Exception e)
                    {
                        // process the error
                    }
                    finally
                    {
                        if(bsd != null)
                        {
                            bsd.release();
                        }
                    }
                }

            });
            t.start();
            
        }
    }

    private void BSDResponse(Message msg)
    {
        if (msg instanceof ByteMessage)
        {
            ByteMessage reply = (ByteMessage) msg;
            _result = (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) {
                // process the error
            }
            if(data != null)
            {
                _result = new String(data);
            }
        }

        _app.invokeLater(new Runnable() {

            public void run() {
                _app.pushScreen(new HTTPOutputScreen(_result));
            }

        });

    }

}


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.