networkingtest.java

来自「大量j2me源代码」· Java 代码 · 共 77 行

JAVA
77
字号
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;

/**
 * A demonstration MIDlet which shows the basics of HTTP networking.
 * @author Martin J. Wells
 */
public class NetworkingTest extends javax.microedition.midlet.MIDlet
{
   private Form form;

   /**
    * MIDlet constructor that instantiates a form and then opens up an HTTP
    * connection to java.sun.com. It then reads a few bytyes and adds those as
    * a string to the form.
    * @throws IOException if a networking error occurs
    */
   public NetworkingTest() throws IOException
   {
      // Setup the UI
      form = new Form("Http Dump");

      // Create a HTTP connection to the java.sun.com site
      InputStream inStream = Connector.openInputStream("http://java.sun.com/");

      // Open the result and stream the first chunk into a byte buffer
      byte[] buffer = new byte[255];
      int bytesRead = inStream.read(buffer);
      if (bytesRead > 0)
      {
         inStream.close();

         // Turn the result into a string and display it
         String webString = new String(buffer, 0, bytesRead);
         form.append(webString);
      }
   }

   /**
    * Called by the Application Manager when the MIDlet is starting or resuming
    * after being paused. In this example it acquires the current Display object
    * and uses it to set the Form object created in the MIDlet constructor as
    * the active Screen to display.
    * @throws MIDletStateChangeException
    */
   protected void startApp() throws MIDletStateChangeException
   {
      // display our UI
      Display.getDisplay(this).setCurrent(form);
   }

   /**
    * Called by the MID's Application Manager to pause the MIDlet. A good
    * example of this is when the user receives an incoming phone call whilst
    * playing your game. When they're done the Application Manager will call
    * startApp to resume. For this example we don't need to do anything.
    */
   protected void pauseApp()
   {
   }

   /**
    * Called by the MID's Application Manager when the MIDlet is about to
    * be destroyed (removed from memory). You should take this as an opportunity
    * to clear up any resources and save the game. For this example we don't
    * need to do anything.
    * @param unconditional if false you have the option of throwing a
    * MIDletStateChangeException to abort the destruction process.
    * @throws MIDletStateChangeException
    */
   protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
   }
}

⌨️ 快捷键说明

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