example.http
Class HttpView

java.lang.Object
  extended by javax.microedition.midlet.MIDlet
      extended by example.http.HttpView
All Implemented Interfaces:
java.lang.Runnable, javax.microedition.lcdui.CommandListener

public class HttpView
extends javax.microedition.midlet.MIDlet
implements javax.microedition.lcdui.CommandListener, java.lang.Runnable

An example MIDlet to fetch a page using an HttpConnection. Refer to the startApp, pauseApp, and destroyApp methods so see how it handles each requested transition.


Field Summary
(package private)  javax.microedition.lcdui.Command aboutCommand
          user interface command to request copyright information.
(package private)  javax.microedition.lcdui.Alert alert
          user interface alert component.
(package private)  javax.microedition.lcdui.Command backCommand
          user interface command to return back to previous screen.
(package private)  javax.microedition.lcdui.Command cancelCommand
          user interface command to cancel the current screen.
(package private)  javax.microedition.lcdui.TextBox content
          user interface text box for the contents of the fetched URL.
(package private)  javax.microedition.lcdui.Display display
          current display.
(package private)  javax.microedition.lcdui.Command errorsCommand
          user interface command to display errors from current request.
(package private)  javax.microedition.lcdui.Form errorsForm
          form to display exceptions
(package private)  javax.microedition.lcdui.Command exitCommand
          user interface command for indicating Exit request.
(package private)  javax.microedition.lcdui.Command getCommand
          user interface command to request an HTTP GET transaction.
(package private)  javax.microedition.lcdui.Command headCommand
          user interface command to request an HTTP HEAD transaction.
(package private)  javax.microedition.lcdui.Form headerForm
          user interface screen for HTTP headers
(package private)  javax.microedition.lcdui.Command headersCommand
          user interface command to request current HTTP headers.
(package private)  javax.microedition.lcdui.Command helpCommand
          user interface command to display help message.
(package private)  javax.microedition.lcdui.Command newURLCommand
          user interface command to enter a new URL
(package private)  javax.microedition.lcdui.Command okCommand
          user interface command to confirm current screen.
(package private)  javax.microedition.lcdui.Command postCommand
          user interface command to request an HTTP POST transaction.
(package private)  javax.microedition.lcdui.Form progressForm
          user interface form to hold progress results.
(package private)  javax.microedition.lcdui.Gauge progressGauge
          user interface progress indicator.
(package private)  javax.microedition.lcdui.Command reloadCommand
          user interface command for indicating a page reload request.
(package private)  javax.microedition.lcdui.Command removeURLCommand
          user interface command to remove the current URL
(package private)  javax.microedition.lcdui.Command requestCommand
          current HTTP request type - GET, HEAD, or POST
(package private)  javax.microedition.lcdui.Form requestForm
          form to display request including parsing
(package private)  javax.microedition.lcdui.Command requestsCommand
          user interface command to display current HTTP request headers.
(package private)  java.lang.Thread thread
          instance of a thread for asynchronous networking and user interface.
(package private)  java.lang.String url
          current requested url.
(package private)  javax.microedition.lcdui.TextBox urlbox
          data entry text box for inputting URLs
(package private)  javax.microedition.lcdui.List urlList
          user interface component containing a list of URLs
(package private)  java.util.Vector urls
          array of current URLs
 
Constructor Summary
HttpView()
          initialize the MIDlet with the current display object.
 
Method Summary
(package private)  void cleanUp(javax.microedition.io.HttpConnection conn, java.io.InputStream input, java.io.OutputStream output)
          Clean up all objects used by the HttpConnection.
(package private)  void clearForm(javax.microedition.lcdui.Form form)
          Clear out all items in a Form.
 void commandAction(javax.microedition.lcdui.Command c, javax.microedition.lcdui.Displayable s)
          Respond to commands, including exit
 void destroyApp(boolean unconditional)
          Destroy must cleanup everything.
(package private)  void genErrorsForm(java.lang.String s, java.lang.Throwable ex)
          Set the Alert to the exception message and display it.
(package private)  void genHeaderForm(javax.microedition.io.HttpConnection c)
          Generate and fill in the Form with the header fields.
(package private)  void genProgressForm(java.lang.String name, java.lang.String url)
          Generate the options form with URL title and progress gauge.
(package private)  void genRequestForm(javax.microedition.io.HttpConnection c)
          Generate the form with the request attributes and values.
(package private)  javax.microedition.io.HttpConnection handleRedirects(javax.microedition.io.HttpConnection c)
          Check for redirect response codes and handle the redirect by getting the new location and opening a new connection to it.
 void pauseApp()
          Pause signals the thread to stop by clearing the thread field.
 void run()
          Fetch the specified url in a separate thread and update the progress bar as it goes.
(package private)  void setConfig(javax.microedition.io.HttpConnection c)
          Add request properties for the configuration, profiles, and locale of this system.
(package private)  void setupList()
          Check the attributes in the descriptor that identify url's and titles and initialize the lists of urls and urlList.
(package private)  void showAlert(java.lang.String s, javax.microedition.lcdui.Screen next)
          Set the alert string and display it.
 void startApp()
          Start creates the thread to do the timing.
 
Methods inherited from class javax.microedition.midlet.MIDlet
checkPermission, getAppProperty, notifyDestroyed, notifyPaused, platformRequest, resumeRequest
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

exitCommand

javax.microedition.lcdui.Command exitCommand
user interface command for indicating Exit request.


reloadCommand

javax.microedition.lcdui.Command reloadCommand
user interface command for indicating a page reload request.


headCommand

javax.microedition.lcdui.Command headCommand
user interface command to request an HTTP HEAD transaction.


postCommand

javax.microedition.lcdui.Command postCommand
user interface command to request an HTTP POST transaction.


getCommand

javax.microedition.lcdui.Command getCommand
user interface command to request an HTTP GET transaction.


aboutCommand

javax.microedition.lcdui.Command aboutCommand
user interface command to request copyright information.


cancelCommand

javax.microedition.lcdui.Command cancelCommand
user interface command to cancel the current screen.


backCommand

javax.microedition.lcdui.Command backCommand
user interface command to return back to previous screen.


headersCommand

javax.microedition.lcdui.Command headersCommand
user interface command to request current HTTP headers.


requestsCommand

javax.microedition.lcdui.Command requestsCommand
user interface command to display current HTTP request headers.


errorsCommand

javax.microedition.lcdui.Command errorsCommand
user interface command to display errors from current request.


newURLCommand

javax.microedition.lcdui.Command newURLCommand
user interface command to enter a new URL


removeURLCommand

javax.microedition.lcdui.Command removeURLCommand
user interface command to remove the current URL


okCommand

javax.microedition.lcdui.Command okCommand
user interface command to confirm current screen.


helpCommand

javax.microedition.lcdui.Command helpCommand
user interface command to display help message.


urlList

javax.microedition.lcdui.List urlList
user interface component containing a list of URLs


urls

java.util.Vector urls
array of current URLs


alert

javax.microedition.lcdui.Alert alert
user interface alert component.


content

javax.microedition.lcdui.TextBox content
user interface text box for the contents of the fetched URL.


display

javax.microedition.lcdui.Display display
current display.


thread

java.lang.Thread thread
instance of a thread for asynchronous networking and user interface.


url

java.lang.String url
current requested url.


requestCommand

javax.microedition.lcdui.Command requestCommand
current HTTP request type - GET, HEAD, or POST


progressForm

javax.microedition.lcdui.Form progressForm
user interface form to hold progress results.


progressGauge

javax.microedition.lcdui.Gauge progressGauge
user interface progress indicator.


headerForm

javax.microedition.lcdui.Form headerForm
user interface screen for HTTP headers


requestForm

javax.microedition.lcdui.Form requestForm
form to display request including parsing


errorsForm

javax.microedition.lcdui.Form errorsForm
form to display exceptions


urlbox

javax.microedition.lcdui.TextBox urlbox
data entry text box for inputting URLs

Constructor Detail

HttpView

public HttpView()
initialize the MIDlet with the current display object.

Method Detail

startApp

public void startApp()
Start creates the thread to do the timing. It should return immediately to keep the dispatcher from hanging.

Specified by:
startApp in class javax.microedition.midlet.MIDlet

pauseApp

public void pauseApp()
Pause signals the thread to stop by clearing the thread field. If stopped before done with the iterations it will be restarted from scratch later.

Specified by:
pauseApp in class javax.microedition.midlet.MIDlet

destroyApp

public void destroyApp(boolean unconditional)
Destroy must cleanup everything. The thread is signaled to stop and no result is produced.

Specified by:
destroyApp in class javax.microedition.midlet.MIDlet
Parameters:
unconditional - true if a forced shutdown was requested

setupList

void setupList()
Check the attributes in the descriptor that identify url's and titles and initialize the lists of urls and urlList.

The attributes are named "ViewTitle-n" and "ViewURL-n". The value "n" must start at "1" and increment by 1.


commandAction

public void commandAction(javax.microedition.lcdui.Command c,
                          javax.microedition.lcdui.Displayable s)
Respond to commands, including exit

Specified by:
commandAction in interface javax.microedition.lcdui.CommandListener
Parameters:
c - user interface command requested
s - screen object initiating the request

run

public void run()
Fetch the specified url in a separate thread and update the progress bar as it goes. If the user cancels the fetch, the thread be changed from this thread. If this happens no further updates should be made to the displayable forms. Those shared objects may be re-used by the next fetch.

Specified by:
run in interface java.lang.Runnable

cleanUp

void cleanUp(javax.microedition.io.HttpConnection conn,
             java.io.InputStream input,
             java.io.OutputStream output)
Clean up all objects used by the HttpConnection. We must close the InputStream, OutputStream objects, as well as the HttpConnection object, to reclaim system resources. Otherwise, we may not be able to make new connections on some platforms.

Parameters:
conn - the HttpConnection
input - the InputStream of the HttpConnection, may be null if it's not yet opened.
output - the OutputStream the HttpConnection, may be null if it's not yet opened.

handleRedirects

javax.microedition.io.HttpConnection handleRedirects(javax.microedition.io.HttpConnection c)
                                               throws java.io.IOException
Check for redirect response codes and handle the redirect by getting the new location and opening a new connection to it. The original connection is closed. The process repeats until there are no more redirects.

Parameters:
c - the initial HttpConnection
Returns:
the final HttpConnection
Throws:
java.io.IOException

setConfig

void setConfig(javax.microedition.io.HttpConnection c)
         throws java.io.IOException
Add request properties for the configuration, profiles, and locale of this system.

Parameters:
c - current HttpConnection to receive user agent header
Throws:
java.io.IOException

genHeaderForm

void genHeaderForm(javax.microedition.io.HttpConnection c)
             throws java.io.IOException
Generate and fill in the Form with the header fields.

Parameters:
c - the open connection with the result headers.
Throws:
java.io.IOException

genRequestForm

void genRequestForm(javax.microedition.io.HttpConnection c)
              throws java.io.IOException
Generate the form with the request attributes and values.

Parameters:
c - the open connection with the request headers.
Throws:
java.io.IOException

genProgressForm

void genProgressForm(java.lang.String name,
                     java.lang.String url)
Generate the options form with URL title and progress gauge.

Parameters:
name - the title of the URL to be loaded.
url - label for the progress gauge

genErrorsForm

void genErrorsForm(java.lang.String s,
                   java.lang.Throwable ex)
Set the Alert to the exception message and display it.

Parameters:
s - the Exception title string
ex - the Exception

showAlert

void showAlert(java.lang.String s,
               javax.microedition.lcdui.Screen next)
Set the alert string and display it.

Parameters:
s - the error message
next - the screen to be shown after the Alert.

clearForm

void clearForm(javax.microedition.lcdui.Form form)
Clear out all items in a Form.

Parameters:
form - the Form to clear.