Help Center

Local Navigation

Code sample: Creating a connection over HTTP by using the first available transport

/*
* HTTPFirstAvailable.java
*
* Research In Motion Limited proprietary and confidential
* Copyright Research In Motion Limited, 2010
*/
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.io.transport.*;
import javax.microedition.io.*;
import java.io.*;

public class HTTPFirstAvailable extends UiApplication
{   
    public static void main(String[] args)
    {
        HTTPFirstAvailable theApp = new HTTPFirstAvailable();
        theApp.enterEventDispatcher();
    }
    
    public HTTPFirstAvailable()
    {
        pushScreen(new HTTPFirstAvailableScreen());
    }
}

class ConnectionThread extends Thread
{
    public void run()
    {
         
          ConnectionFactory connFact = new ConnectionFactory();
          ConnectionDescriptor connDesc;
          connDesc = connFact.getConnection("http://www.example.com");
          if (connDesc != null)
          {
              HttpConnection httpConn;
              httpConn = (HttpConnection)connDesc.getConnection();
              try
              {
                  final int iResponseCode = httpConn.getResponseCode();
                  UiApplication.getUiApplication().invokeLater(new Runnable()
                  {
                      public void run()
                      {
                          Dialog.alert("Response code: " + 
                                        Integer.toString(iResponseCode));
                      }
                   });
               } 
               catch (IOException e) 
               {
                 System.err.println("Caught IOException: " 
                      + e.getMessage());
               }
          }
    }
}    


class HTTPFirstAvailableScreen extends MainScreen
{   
    public HTTPFirstAvailableScreen()
    {
        setTitle("HTTP First Sample");
        add(new RichTextField("Trying to make HTTP connection... \n"));
        ConnectionThread ct = new ConnectionThread();
        ct.start();
    }
}

Was this information helpful? Send us your comments.