Help Center

Local Navigation

Send form data to a web address in a browser field

  1. Import the required classes and interfaces.
    import net.rim.device.api.browser.field2.*;
    import net.rim.device.api.io.http.*;
    import net.rim.device.api.system.*;
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.container.*;
    import java.lang.*;
    import java.util.*;
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The BrowserFieldDemoScreen class, described in step 3, represents the custom screen.
    public class BrowserFieldDemo extends UiApplication
    {
         public static void main(String[] args)
         {
              BrowserFieldDemo app = new BrowserFieldDemo();
              app.enterEventDispatcher();
         }
    
         public BrowserFieldDemo() 
         {
              pushScreen(new BrowserFieldDemoScreen());
         }
    }
  3. Create the custom screen by extending the MainScreen class.
    class BrowserFieldDemoScreen extends MainScreen
    {
         public BrowserFieldDemoScreen()
         {
         }
    }
  4. In the screen constructor, create an instance of the BrowserField class.
    BrowserField browserField = new BrowserField();
  5. In the screen constructor, invoke add() to add the BrowserField object to the screen.
    add(browserField);
  6. In the screen constructor, create a String object that contains the base web address of the web page that you are sending the form data to.
    String baseURL = "http://www.blackberry.com";
  7. In the screen constructor, create a String that specifies the form data that your application sends to the web page.
    String postData = "fieldname1=value1&fieldname2=value2";
  8. In the screen constructor, create a Hashtable object to store the header information for the form data.
    Hashtable header = new Hashtable();
  9. In the screen constructor, invoke Hashtable.put() to specify the header information for the form data.
    header.put("Content-Length", "" + postData.length());
    header.put("Content-Type", "application/x-www-form-urlencoded");
  10. In the screen constructor, invoke BrowserField.requestContent() to send the form data to the web page and display the web page.
    browserField.requestContent(baseURL, postData.getBytes(), new HttpHeaders(header));

Code sample: Sending form data to a web address in a browser field

import net.rim.device.api.browser.field2.*;
import net.rim.device.api.io.http.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import java.lang.*;
import java.util.*;

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

class BrowserFieldDemoScreen extends MainScreen
{
    public BrowserFieldDemoScreen()
    {
        BrowserField browserField = new BrowserField();}
        add(browserField);
        String baseURL = "http://www.blackberry.com";
        String postData = "fieldname1=value1&fieldname2=value2";
        Hashtable header = new Hashtable();
        header.put("Content-Length", "" + postData.length());
        header.put("Content-Type", "application/x-www-form-urlencoded");
        browserField.requestContent(baseURL, postData.getBytes(), new HttpHeaders(header));
    }
}
Next topic: Dialog boxes

Was this information helpful? Send us your comments.