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

HttpExample

public class HttpExample extends MIDlet implements Runnable
sample http example MIDlet.

Fields Summary
String
url
example URL for HTTP GET
StringBuffer
buffer
string buffer for assembling HTTP requests.
javax.microedition.lcdui.Gauge
gauge
user interface component for displaying network progress.
javax.microedition.lcdui.Form
form
user interface screen for displaying progress gauge.
Constructors Summary
public HttpExample()
Create the progress form and gauge. This program is not interactive, it will exit when done.


                         
      
        gauge = new Gauge("Progress", false, 10, 0);
        form = new Form("Progress");
        form.append(gauge);           
        Display.getDisplay(this).setCurrent(form);
    
Methods Summary
public voiddestroyApp(boolean unconditional)
Destroy must cleanup everything. The thread is signaled to stop and no result is produced.

param
unconditional true if forced shutdown.

    
voidgetViaContentConnection(java.lang.String url)
Simple read of a url using ContentConnection. No HTTP specific behavior is needed or used.

Connector.open is used to open url and a ContentConnection is returned. The ContentConnection may be able to provide the length. If the length is available, it is used to read the data in bulk. From the StreamConnection the InputStream is opened. It is used to read every character until end of file (-1). If an exception is thrown the connection and stream is closed.

param
url the URL to process.

	ContentConnection c = null;
	InputStream is = null;
	try {
	    c = (ContentConnection)Connector.open(url);
            is = c.openInputStream();
            buffer.setLength(0);

            int len = (int)c.getLength();
	    if (len > 0) {
		byte[] data = new byte[len];
		int actual = is.read(data);
		process(data);
	    } else {
		int ch;
		while ((ch = is.read()) != -1) {
		    process((byte)ch);
		}
	    }
	} finally {
	    if (is != null)
		is.close();
	    if (c != null)
		c.close();
	}
    
voidgetViaHttpConnection(java.lang.String url)
Read the HTTP headers and the data using HttpConnection. Check the response code to insure successful retrieval.

Connector.open is used to open url and a HttpConnection is returned. The HTTP headers are read and processed. If the length is available, it is used to read the data in bulk. From the HttpConnection the InputStream is opened. It is used to read every character until end of file (-1). If an exception is thrown the connection and stream is closed.

param
url the URL to process.

	HttpConnection c = null;
	InputStream is = null;
	OutputStream os = null;
	try {
	    int status = -1;

	    // Open the connection and check for re-directs
	    while (true) {
		c = (HttpConnection)Connector.open(url);
		setRequestHeaders(c);

		// Get the status code, causing the connection to be made
		status = c.getResponseCode();
		if (status == HttpConnection.HTTP_TEMP_REDIRECT ||
		    status == HttpConnection.HTTP_MOVED_TEMP ||
		    status == HttpConnection.HTTP_MOVED_PERM) {
		    // Get the new location and close the connection
		    url = c.getHeaderField("location");
		    c.close();

		    System.out.println("Redirecting to " + url);
		} else {
		    break;
		}
	    }
            
            // Any 500 status number (500, 501) means there was a server error 
	    if ((status == HttpConnection.HTTP_NOT_IMPLEMENTED) ||
                (status == HttpConnection.HTTP_VERSION) ||
                (status == HttpConnection.HTTP_INTERNAL_ERROR) ||
                (status == HttpConnection.HTTP_GATEWAY_TIMEOUT) ||
                (status == HttpConnection.HTTP_BAD_GATEWAY)) {
                System.err.print("WARNING: Server error status ["+status+"] ");
                System.err.println("returned for url ["+url+"]");
                if (is != null)
                    is.close();
                if (os != null)
                    os.close();
                if (c != null)
                    c.close();
                return;
            }

            // Only HTTP_OK (200) means the content is returned.
            if (status != HttpConnection.HTTP_OK) {
                throw new IOException("Response status not OK ["+status+"]");
            }

	    // Get the ContentType
	    String type = c.getType();
	    processType(type);

	    // open the InputStream 
	    is = c.openInputStream();
            buffer.setLength(0);

            // Get the length and process the data
	    int len = (int)c.getLength();
	    if (len > 0) {
		byte[] data = new byte[len];
		int actual = is.read(data);
		process(data);
	    } else {
		int ch;
		while ((ch = is.read()) != -1) {
		    process((byte)ch);
		}
	    }
	} finally {
	    if (is != null)
		is.close();
	    if (c != null)
		c.close();
	}
    
voidgetViaStreamConnection(java.lang.String url)
Simple read of a url using StreamConnection. No HTTP specific behavior is needed or used.

Connector.open is used to open url and a StreamConnection is returned. From the StreamConnection the InputStream is opened. It is used to read every character until end of file (-1). If an exception is thrown the connection and stream is closed.

param
url the URL to process.

	StreamConnection c = null;
	InputStream s = null;
	try {
	    c = (StreamConnection)Connector.open(url);
	    s = c.openInputStream();
            buffer.setLength(0);

            int ch;
	    while ((ch = s.read()) != -1) {
		process((byte)ch);
	    }
	} finally {
	    if (s != null) 
		s.close();
	    if (c != null)
		c.close();
	}
    
public voidpauseApp()
Pause, discontinue with the http tests

	
    
voidpostViaHttpConnection(java.lang.String url)
Post a request with some headers and content to the server and process the headers and content.

Connector.open is used to open url and a HttpConnection is returned. The request method is set to POST and request headers set. A simple command is written and flushed. The HTTP headers are read and processed. If the length is available, it is used to read the data in bulk. From the StreamConnection the InputStream is opened. It is used to read every character until end of file (-1). If an exception is thrown the connection and stream is closed.

param
url the URL to process.

	int status = 0;
	HttpConnection c = null;
	InputStream is = null;
	OutputStream os = null;

	try {
	    c = (HttpConnection)Connector.open(url);

	    // Set the request method and headers
	    c.setRequestMethod(HttpConnection.POST);
	    c.setRequestProperty("If-Modified-Since",
		"29 Oct 1999 19:43:31 GMT");
	    c.setRequestProperty("User-Agent",
		"Profile/MIDP-1.0 Configuration/CLDC-1.0");
	    c.setRequestProperty("Content-Language", "en-US");

	    // Getting the output stream may flush the headers
	    os = c.openOutputStream();
	    os.write("LIST games\n".getBytes());
	    os.flush();		// Optional, openInputStream will flush

	    // Get the status code, causing the connection to be made
	    status = c.getResponseCode();

            // Any 500 status number (500, 501) means there was a server error 
	    if ((status == HttpConnection.HTTP_NOT_IMPLEMENTED) ||
                (status == HttpConnection.HTTP_VERSION) ||
                (status == HttpConnection.HTTP_INTERNAL_ERROR) ||
                (status == HttpConnection.HTTP_GATEWAY_TIMEOUT) ||
                (status == HttpConnection.HTTP_BAD_GATEWAY)) {
                System.err.print("WARNING: Server error status ["+status+"] ");
                System.err.println("returned for url ["+url+"]");
                if (is != null)
                    is.close();
                if (os != null)
                    os.close();
                if (c != null)
                    c.close();
                return;
            }
                

            // Only HTTP_OK (200) means the content is returned.
            if (status != HttpConnection.HTTP_OK) {                
		throw new IOException("Response status not OK ["+status+"]");
	    }

	    // Open the InputStream and get the ContentType
	    is = c.openInputStream();
	    String type = c.getType();
	    processType(type);

	    // Get the length and process the data
	    int len = (int)c.getLength();
	    if (len > 0) {
		byte[] data = new byte[len];
		int actual = is.read(data);
		process(data);
	    } else {
		int ch;
		while ((ch = is.read()) != -1) {
		    process((byte)ch);
		}
	    }
	} finally {
	    if (is != null)
		is.close();
	    if (os != null)
		os.close();
	    if (c != null)
		c.close();
	}
    
voidprocess(byte b)
Process the data one character at a time.

param
b one byte of data

	buffer.append((char)b);
    
voidprocess(byte[] b)
Process the data from the array.

param
b an array of bytes.

	for (int i = 0; i < b.length; i++) {
	    process(b[i]);
	}
    
voidprocessType(java.lang.String type)
Process the type.

param
type that type

    
public voidrun()
Run the examples.

;
	try {
            gauge.setLabel("Get using ContentConnection");
            gauge.setValue(2);
	    getViaContentConnection(url);

            gauge.setLabel("Get using StreamConnection");
            gauge.setValue(4);
            getViaStreamConnection(url);

            gauge.setLabel("Get using HttpConnection");
            gauge.setValue(6);
	    getViaHttpConnection(url);
            
            gauge.setLabel("Post using HttpConnection");
            gauge.setValue(8);
            postViaHttpConnection(url);
	} catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
        gauge.setValue(10);
        notifyDestroyed();
    
voidsetRequestHeaders(javax.microedition.io.HttpConnection c)
Add request properties for the configuration, profiles, and locale of this system.

param
c current HttpConnection to apply request headers

        String conf = System.getProperty("microedition.configuration");
        String prof = System.getProperty("microedition.profiles");
	int space = prof.indexOf(' ");
	if (space != -1) {
	    prof = prof.substring(0, space -1);
	}
        String locale = System.getProperty("microedition.locale");

        String ua = "Profile/" + prof +
                " Configuration/" + conf;
        c.setRequestProperty("User-Agent", ua);
        
        if (locale != null) {
            c.setRequestProperty("Content-Language", locale);
        }
    
public voidstartApp()
Start a thread to run the examples.

        new Thread(this).start();