FileDocCategorySizeDatePackage
PushExample.javaAPI DocJ2ME MIDP 2.05807Thu Nov 07 12:02:16 GMT 2002example.http

PushExample

public class PushExample extends MIDlet implements CommandListener
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.

Fields Summary
Command
exitCommand
user interface command for indicating Exit request.
List
urlList
user interface component containing a list of URLs
Vector
urls
array of current URLs
Alert
alert
user interface alert component.
static final int
DefaultTimeout
Image
newsHoundImage
boolean
imageLoaded
Display
display
current display.
String
url
current requested url.
Constructors Summary
public PushExample()
initialize the MIDlet with the current display object.

			

             
      
        display = Display.getDisplay(this);

    
Methods Summary
public voidcommandAction(Command c, Displayable s)
Respond to commands, including exit

param
c user interface command requested
param
s screen object initiating the request

        try {
            if (c == exitCommand) {
                destroyApp(false);
                notifyDestroyed();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    
public voiddestroyApp(boolean unconditional)
Destroy must cleanup everything. The thread is signaled to stop and no result is produced.

param
unconditional true if a forced shutdown was requested

    
public voidpauseApp()
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.

    
voidsetupList()
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.

        urls = new Vector();
        urlList = new List("News Headlines", List.IMPLICIT);
	urlList.setFitPolicy(Choice.TEXT_WRAP_OFF);
        urlList.addCommand(exitCommand);
        urlList.setCommandListener(this);

        for (int n = 1; n < 100; n++) {
            String nthURL = "ViewURL-"+ n;
            String url = getAppProperty(nthURL);
            if (url == null || url.length() == 0) {
		break;
            }

            String nthTitle = "ViewTitle-" + n;
            String title = getAppProperty(nthTitle);
            if (title == null || title.length() == 0) {
		title = url;
            }

            urls.addElement(url);
            urlList.append(title, null);
        }

        urlList.append("Next InComing News: ", null);
    
public voidstartApp()
Start creates the thread to do the timing. It should return immediately to keep the dispatcher from hanging.


       try {
            newsHoundImage = Image.createImage("/example/http/images/newshound.png");
	    imageLoaded = true;
        } catch (java.io.IOException ex) {
	    System.err.println("Image is not loaded :" + imageLoaded);
        }


        alert = new Alert("News Hound", "", newsHoundImage, AlertType.INFO);

        alert.setTimeout(DefaultTimeout);
        setupList();


	/* Bytes read from the URL update connection. */
	int count;
	/* Check for inbound async connection for sample Finger port. */
	String[] connections = PushRegistry.listConnections(true);

	/* HttpView was started to handle inbound request. */
	String pushProperty = getAppProperty("MIDlet-Push-1");

	if (connections != null && connections.length > 0) {
	    String newurl = "Pushed URL Placeholder";

	    /* DEBUG: Test basic get registry information interfaces. */
	    try {
		String midlet = PushRegistry.getMIDlet(connections[0]);
		String filter = PushRegistry.getFilter(connections[0]);

	    } catch (Exception e) {
		e.printStackTrace();
	    }
		    
	    /* Check for socket or datagram connection. */
	    if (connections[0].startsWith("socket://")) {
		try {
		    /* Simple test assumes a server socket connection. */
		    ServerSocketConnection scn = (ServerSocketConnection)
			Connector.open(connections[0]);
		    SocketConnection sc = (SocketConnection)
			scn.acceptAndOpen();
		    
		    /* Read one line of text as a new URL to add to the list. */
		    DataInputStream dis = sc.openDataInputStream();
		    byte[] buf = new byte[256];
		    int endofline = 0;
		    count =	dis.read(buf);
		    for (int i = 0; i < count; i++) {
			if (buf[i] == '\n") {
			    endofline = i;
			    break;
			}
		    }
		    newurl = new String(buf, 0, endofline);

		    dis.close();

		    sc.close();
		    scn.close();
		} catch (IOException e) {
		    System.err.println("******* io exception in push example");
		    e.printStackTrace();
		}		
	    } else {
		System.err.println(" NYI - unknown connection type");
	    }
	    urlList.append(newurl, null);
	    urls.addElement(newurl);
	} else {
	    connections = PushRegistry.listConnections(false);	   
	}

	display.setCurrent(alert, urlList);