⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpview.java

📁 用于移动设备上的java虚拟机源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)HttpView.java	1.30 02/07/25 @(#) * * Copyright (c) 1999-2002 Sun Microsystems, Inc.  All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. */package example.http;import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.DataInputStream;import java.util.Vector;import java.util.Date;import java.lang.String;import example.About;/** * 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. */public class HttpView extends MIDlet implements CommandListener, Runnable {    /** user interface command for indicating Exit request. */    Command exitCommand  = new Command("Exit", Command.EXIT, 2);    /** user interface comand for indicating a page reload request. */    Command reloadCommand = new Command("Reload", Command.SCREEN, 1);    /** user interface command to request an HTTP HEAD transaction. */    Command headCommand = new Command("Head", Command.SCREEN, 1);    /** user interface command to request an HTTP POST transaction. */    Command postCommand = new Command("Post", Command.SCREEN, 1);    /** user interfrace command to request an HTTP GET transaction. */    Command getCommand = new Command("Get", Command.SCREEN, 1);    /** user interface command to request copyright information. */    Command aboutCommand = new Command("About", Command.HELP, 1);    /** user interface command to cancel the current screen. */    Command cancelCommand = new Command("Cancel", Command.SCREEN, 1);    /** user interface command to return back to previous screen. */    Command backCommand = new Command("Back", Command.BACK, 1);    /** user interface command to request current HTTP headers. */    Command headersCommand = new Command("Headers", Command.SCREEN, 1);    /** user interface command to display current HTTP request headers. */    Command requestsCommand = new Command("Requests", Command.SCREEN, 1);    /** user interface command to display errors from current request. */    Command errorsCommand = new Command("Errors", Command.SCREEN, 1);    /** user interface command to enter a new URL */    Command newURLCommand = new Command("New URL", Command.SCREEN, 10);    /** user inetrface command to remove the current URL */    Command removeURLCommand = new Command("Remove", Command.SCREEN, 11);    /** user interface command to confirm current screen. */    Command okCommand = new Command("Ok", Command.SCREEN, 1);    /** user interface command to display help message. */    Command helpCommand = new Command("Help", Command.HELP, 1);    /** user interface component containing a list of URLs */    List urlList;    /** array of current URLs */    Vector urls;    /** user interface alert component. */    Alert alert;    /** user interface text box for the contents of the fetched URL. */    TextBox content;    /** current display. */    Display display;    /** instance of a thread for asynchronous networking and user interface. */    Thread thread;    /** current requested url. */    String url;			    /** current HTTP request type - GET, HEAD, or POST */    Command requestCommand;	    /** user interface form to hold progress results. */    Form progressForm;    /** user interface progress indicator. */    Gauge progressGauge;    /** user interface screen for HTTP headers */    Form headerForm;    /** form to display request including parsing */    Form requestForm;              /** form to display exceptions */    Form errorsForm;		    /** data entry text box for inputting URLs */    TextBox urlbox;    /** initialize the MIDlet with the current display object. */    public HttpView() {        display = Display.getDisplay(this);        setupList();        alert = new Alert("Warning");        alert.setTimeout(2000);        headerForm = new Form("Headers");        headerForm.addCommand(backCommand);        headerForm.addCommand(requestsCommand);        headerForm.setCommandListener(this);        requestForm = new Form("Request headers");        requestForm.addCommand(backCommand);        requestForm.addCommand(errorsCommand);        requestForm.setCommandListener(this);        progressForm = new Form("Progress");        progressForm.addCommand(cancelCommand);        progressForm.setCommandListener(this);        progressGauge = new javax.microedition.lcdui.Gauge(url, false, 9, 0);        progressForm.append(progressGauge);        errorsForm = new Form("Errors");        errorsForm.addCommand(backCommand);        errorsForm.addCommand(headersCommand);        errorsForm.setCommandListener(this);                urlbox = new TextBox("Enter Url", "http://", 400, TextField.URL);        urlbox.addCommand(okCommand);        urlbox.setCommandListener(this);    }    /**     * Start creates the thread to do the timing.     * It should return immediately to keep the dispatcher     * from hanging.     */    public void startApp() {	/* 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) {		    e.printStackTrace();		}				/* 		 * After successfully receiving a socket posted URL		 * register a datgram connection, too.		 */		try {		    PushRegistry.registerConnection("datagram://:40080",						    "example.http.HttpView",						    "129.148.*.*");		} catch (ClassNotFoundException e) {		    e.printStackTrace();		} catch (IllegalArgumentException e) {		    e.printStackTrace();		} catch (ConnectionNotFoundException e) {		    e.printStackTrace();		} catch (IOException e) {		    e.printStackTrace();		}			    } else if (connections[0].startsWith("datagram://")) {		/* Must be a datagram connection. */		try {		    UDPDatagramConnection udc = (UDPDatagramConnection)			Connector.open(connections[0]);		    Datagram dg = udc.newDatagram(256);		    udc.receive(dg);		    udc.close();		    		    byte[] buf = dg.getData();		    int endofline = 0;		    count =	buf.length;		    for (int i = 0; i < count; i++) {			if (buf[i] == '\n') {			    endofline = i;			    break;			}		    }		    newurl = new String(buf, 0, endofline);		    		    /* Unregister the datagram connection. */		    PushRegistry.unregisterConnection("datagram://:40080");		    		} catch (SecurityException e) {		    e.printStackTrace();		} catch (IOException e) {		    e.printStackTrace();		}	    } else {		// NYI - unknown connection type	    }	    urlList.append(newurl, null);	    urls.addElement(newurl);	} else {	    connections = PushRegistry.listConnections(false);	    	    /*	     * If the MIDlet was started manually, set an alarm	     * to restart automatically int one minute.	     */	    try {		Date alarm = new Date();		PushRegistry.registerAlarm("example.http.HttpView", 					   alarm.getTime() + 60000);	    } catch (ClassNotFoundException e) {		e.printStackTrace();	    } catch (ConnectionNotFoundException e) {		e.printStackTrace();	    }	}		if (urlList.size() > 0) {            display.setCurrent(urlList);        } else {            alert.setString("No url's configured.");            display.setCurrent(alert, urlList);        }    }    /**     * 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.     */    public void pauseApp() {    }    /**     * 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 void destroyApp(boolean unconditional) {        thread = null;    }    /**     * Check the attributes in the descriptor that identify     * url's and titles and initialize the lists of urls     * and urlList.     * <P>     * The attributes are named "ViewTitle-n" and "ViewURL-n".     * The value "n" must start at "1" and increment by 1.     */    void setupList() {        urls = new Vector();        urlList = new List("URLs", List.IMPLICIT);        urlList.addCommand(headCommand);        urlList.addCommand(getCommand);        urlList.addCommand(postCommand);        urlList.addCommand(exitCommand);        urlList.addCommand(newURLCommand);        urlList.addCommand(removeURLCommand);        urlList.addCommand(helpCommand);        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);        }        urls.addElement("http://jse.east/Telco/HttpTest.txt");        // urls.addElement(        //         "http://dhcp-70-219:8080/examples/servlet/httpdbexport");        // urls.addElement(        //       "http://jse.east.sun.com/~kfinn/proxy.jar");        // urls.addElement(        //       "http://dhcp-70-219:8080/examples/servlet/HelloWorldKerry");        urlList.append("Test URL", null);    }    /**     * Respond to commands, including exit     * @param c user interface command requested     * @param s screen object initiating the request     */    public void commandAction(Command c, Displayable s) {        try {            if (c == exitCommand) {                destroyApp(false);                notifyDestroyed();            } else if (c == headCommand ||		       c == getCommand ||		       c == postCommand ||		       c == List.SELECT_COMMAND) {                if (c == List.SELECT_COMMAND)		    c = getCommand;                requestCommand = c;                // Display the progress screen and                // start the thread to read the url                int i = urlList.getSelectedIndex();                url = (String)urls.elementAt(i);                genProgressForm("Progress", url);                display.setCurrent(progressForm);                thread = new Thread(this);                thread.start();            } else if (c == headersCommand) {                display.setCurrent(headerForm);            } else if (c == requestsCommand) {                display.setCurrent(requestForm);            } else if (c == errorsCommand) {                display.setCurrent(errorsForm);            } else if (c == backCommand) {                if (s == headerForm || s == requestForm || s == errorsForm) {                    display.setCurrent(content);                } else {                    // Display the list of urls.                    display.setCurrent(urlList);                }            } else if (c == cancelCommand) {                // Signal thread to stop and put an alert.                thread = null;                alert.setString("Loading cancelled.");                display.setCurrent(alert, urlList);            } else if (c == aboutCommand) {                About.showAbout(display);            } else if (c == newURLCommand) {                display.setCurrent(urlbox);            } else if (c == removeURLCommand) {                int i = urlList.getSelectedIndex();                urlList.delete(i);                urls.removeElementAt(i);            } else if (c == okCommand && s == urlbox) {                String newurl = urlbox.getString();                urlList.append(newurl, null);                urls.addElement(newurl);                display.setCurrent(urlList);            } else if (c == helpCommand) {                String helpString =                     "Use Head, Get or Post to download a URL.\n\n" +                    "Use 'New URL' to enter a new URL.";                Alert alert = new Alert(null, helpString, null, null);                alert.setTimeout(Alert.FOREVER);                display.setCurrent(alert);             }        } catch (Exception ex) {            ex.printStackTrace();        }    }    /**     * 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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -