networkingtest.java

来自「此文件是关于手机游戏开发的理论」· Java 代码 · 共 72 行

JAVA
72
字号

import java.util.*;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;

public class NetworkingTest extends javax.microedition.midlet.MIDlet
{
   private Form form;

   public NetworkingTest() throws IOException
   {
      // Setup the UI
      form = new Form("Http Dump");
   }

   protected void pauseApp()
   {
   }

   protected void startApp() throws MIDletStateChangeException
   {
      // display our UI
      Display.getDisplay(this).setCurrent(form);

      // create a new thread for the networking (inline class)
      Thread t = new Thread()
      {
         // declare a run method - not called until the thread is started
         // (see t.start below)
         public void run()
         {
            communicate();
         }
      };

      // start the thread (execute the contents of the run method above)
      t.start();

   }

   protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
   }

   private void communicate()
   {
      try
      {
         // 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);
            System.out.println(webString);
            form.append(webString);
         }
      }
      catch (IOException io)
      {
      }
   }
}

⌨️ 快捷键说明

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