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

📄 rwtest.java

📁 J2ME MIDP 2.0 无线设备编程的一些源码
💻 JAVA
字号:
//RWTest.java
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class RWTest extends MIDlet implements CommandListener
{
    //命令对象
    private Command exitCommand, httpCmd, sockCmd, udpCmd;
    private TextBox tb; //通过TextBox来显示信息
    public RWTest()
    {
        tb =new TextBox("OutputWindow","",120,TextField.ANY);
        exitCommand =new Command("Exit",Command.EXIT,1);
        httpCmd =new Command("httpTest",Command.SCREEN,1);
        sockCmd =new Command("socketTest",Command.SCREEN,1);
        udpCmd =new Command("udpTest",Command.SCREEN,1);
        tb.addCommand(exitCommand);
        tb.addCommand(httpCmd);
        tb.addCommand(sockCmd);
        tb.addCommand(udpCmd);
        tb.setCommandListener(this);
    }
    protected void startApp(  ) throws MIDletStateChangeException
    {
        Display.getDisplay(this).setCurrent(tb);
    }

    protected void pauseApp(  )
    {
    }

    protected void destroyApp( boolean p1 )
    {
    }

    public void commandAction(Command c,Displayable d)
    {
        if (c ==exitCommand)
        {
            destroyApp(false);
            notifyDestroyed();
        }
        else if(c ==httpCmd)
        {
        	new NetTestor();
        }
	}

}

class NetTestor extends Thread
{
	public NetTestor()
	{
		this.start();
	}
    public void run() 
    {
        try 
        {
            socketTest();
            //httpTest();
            //getViaStreamConnection();
            //getViaHttpConnection();
        } catch(Exception e) {System.out.println(e.getMessage());}       
    }
    public void getViaStreamConnection( ) throws IOException 
   	{
   		StringBuffer strbuf=new StringBuffer();
		String url="http://localhost/";
		StreamConnection c = null;
        InputStream s = null;
		try 
        {
			c = (StreamConnection)Connector.open(url);
            s = c.openInputStream();
            int ch;
            while ((ch = s.read()) != -1) 
            {
            	System.out.println("read = " +(char)ch);
            	strbuf.append((char)ch);
			}
		}
		finally
		{
			System.out.println("content = " + strbuf.toString());
             if (s != null)
                 s.close();
             if (c != null)
                 c.close();
         }
	}
    public void getViaHttpConnection() throws IOException 
    {
     	String url="http://localhost/";
         HttpConnection c = null;
         InputStream is = null;
         int rc;

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

             // Getting the response code will open the connection,
             // send the request, and read the HTTP response headers.
             // The headers are stored until requested.
             rc = c.getResponseCode();
             if (rc != HttpConnection.HTTP_OK) {
                 throw new IOException("HTTP response code: " + rc);
             }

             is = c.openInputStream();

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

             // Get the length and process the data
             int len = (int)c.getLength();
             if (len > 0) {
                 int actual = 0;
                 int bytesread = 0 ;
                 byte[] data = new byte[len];
                 while ((bytesread != len) && (actual != -1)) {
                    actual = is.read(data, bytesread, len - bytesread);
                    bytesread += actual;
                 }
             } else {
                 int ch;
                 while ((ch = is.read()) != -1) {
                     System.out.println("read = " +ch);
                 }
             }
         } catch (ClassCastException e) {
             throw new IllegalArgumentException("Not an HTTP URL");
         }
          finally {
             if (is != null)
                 is.close();
             if (c != null)
                 c.close();
         }
     }
	public void httpTest( )
	{
		try
		{
			DataInputStream ins = Connector.openDataInputStream("http://127.0.0.1/cndevforum/");
			for(int i=0;i<10;i++)
			{
				System.out.println("a = " +ins.available());
				Thread.sleep(100);
			}
		}
		catch(Exception e)
		{
			System.out.println("error = "+e.getMessage());
		}
	}
	public void socketTest( )
	{
		try
		{
			//InputStream ins = Connector.openDataInputStream("socket://127.0.0.1:8080");
			OutputStream ous = Connector.openDataOutputStream("socket://127.0.0.1:8080");
			ous.write("GET /\n\n".getBytes());
			for(int i=0;i<10;i++)
			{
				//System.out.println("a = " +ins.available());
				Thread.sleep(100);
			}
		}
		catch(Exception e)
		{
			System.out.println("error = "+e.getMessage());
		}
	}
}

⌨️ 快捷键说明

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