Java > test

Titretest
Postée le30-03-2009
Affichée515
Mini-lien
Description

test

EtatNe contient pas d'erreurs. Ne contient pas d'erreurs.
Code d'insertion
Options
Afficher les numéros de lignes  Mettre la source en plein ecran  Selectionner la source  Partager sur Facebook 
Téléchargement Telecharger en format txt  Telecharger en format pdf  Telecharger en format java
Plein ecran
/**
 * HTTP.java
 *
 * Created on 18 March 2005, 15:18
 */

package HTTP;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
/**
 * An example MIDlet which demonstrates the use of multi-threading
 * with an HTTP connection in one thread and the MIDlet in the main
 * thread.
 *
 * @author Motocoder
 * @version 1.0
 */

public class HTTP extends MIDlet implements CommandListener {
        private final int BUFSIZE = 8192; // HTML raw data Buffer size
        private byte databuf[] = new byte[ BUFSIZE ]; // HTTP raw data buffer
        private Display display; // The display object
        private Form form; // The Form UI object
        private HttpThread hthread; // The HTTP thread object
        private int urlcnt; // The URL count pointer
        private Command exitCmd; // The Exit Command object
        private String urls[] = { "http://www.motorola.com","http://www.motocoder.com"
        }; // The URLs to be accessed
        public HTTP() {
//              initialise objects
                form = new Form("HTTPThread");
                display = Display.getDisplay(this);
                exitCmd = new Command("Exit", 1, Command.EXIT);
                form.addCommand(exitCmd);
                form.setCommandListener(this);
//              Initialise and start the HTTP reader thread
                hthread = new HttpThread();
                hthread.start();
//              set the display
                display.setCurrent(form);
        }
        public void startApp() {
        }
        public void pauseApp() {
        }
        public void destroyApp(boolean unconditional) {
                hthread.killThread();
                notifyDestroyed();
        }
        public void commandAction(Command c, Displayable d) {
                if(c == exitCmd) {
                        destroyApp(true);
                }
        }
//      The HTTP reader thread inner class
        class HttpThread extends Thread {
                private boolean running;
                public HttpThread() {
                        running = true;
                        urlcnt = 0;
                }
                private void getUrl(String url) {
                        HttpConnection conn = null;
                        InputStream is = null;
                        ByteArrayOutputStream bos = null;
                        int rescode = 0;
                        int len = 0;
                        int readlen = 0;
                        try {
                                bos = new ByteArrayOutputStream();
                                form.append("Connecting to " + url +"\n");
                                conn = (HttpConnection)Connector.open(url);
                                form.append("Opening inputstream\n");
                                is = conn.openInputStream();
                                form.append("Reading responsecode\n");
                                rescode = conn.getResponseCode();
                                form.append("responsecode: " + rescode + "\n");
                                len = (int)conn.getLength();
                                form.append("Reading data " + len + " bytes\n");
                                if(len == -1) {
                                        readlen = is.read( databuf, 0, BUFSIZE );
                                        while( readlen != -1 ) {
                                                bos.write( databuf, 0, readlen );
                                                readlen = is.read( databuf, 0, BUFSIZE );
                                        }
                                }
                                else {
                                        readlen = is.read( databuf, 0, BUFSIZE );
                                        int totread = readlen;
                                        while( totread != len ) {
                                                bos.write( databuf, 0, readlen );
                                                readlen = is.read( databuf, 0, BUFSIZE );
                                                if( readlen == -1 ) {
//                                                      An error has occurred while reading, break
//                                                      while loop and signal to application of the //error
                                                        statement
//                                                      Don't forget to close the streams and
//                                                      connections
                                                }
                                                else {
                                                        totread += readlen;
                                                }
                                        }
                                }
                        }
                        catch(IOException ioe) {
                                System.out.println(ioe.toString());
                        }
                        finally {
                                form.append("Closing connection\n");
                                if(is != null) {
                                        try {
                                                is.close();
                                        }
                                        catch(IOException ioe) {
                                                form.append("Error closing inputstream\n" );
                                                form.append(ioe.toString() + "\n");
                                        }
                                }
                                if(conn != null) {
                                        try {
                                                conn.close();
                                        }
                                        catch(IOException ioe) {
                                                form.append("Error closing connection\n" );
                                                form.append(ioe.toString() + "\n");
                                        }
                                }
                        }
                }
                public void killThread() {
                        running = false;
                }
                public void run() {
                        while(running) {
                                try {
                                        getUrl(urls[urlcnt]);
                                        urlcnt++;
                                        if(urlcnt == urls.length) {
                                                urlcnt = 0;
                                        }
                                }
                                catch(Throwable t) {
                                        form.append(t.toString() + "\n");
                                }
                                try {
                                        sleep(10000);
                                }
                                catch(Throwable t) {
                                        form.append(t.toString() + "\n");
                                }
                        }
                }
        }
}