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

📄 simpleusehttp.java

📁 j2me的http应用实例
💻 JAVA
字号:
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;


public class SimpleUseHttp extends MIDlet implements CommandListener,Runnable{
	private Display display;
	private Command exitCmd, streamCmd, contentCmd, backCmd;
	private TextBox url;
	private TextBox msg;
	private Ticker tk;
	private int conType;
	public SimpleUseHttp(){
		url = new TextBox("URL","http://localhost:8080/MIDlets/simplehttp.html",120,TextField.URL);
		tk = new Ticker("being connecting.......");
		msg = new TextBox("content",null,250,TextField.ANY);
		exitCmd = new Command("exit",Command.EXIT,1);
		streamCmd = new Command("StreamConnection",Command.SCREEN,1);
		contentCmd = new Command("ContentConnection",Command.SCREEN,1);
		backCmd = new Command("back",Command.SCREEN,1);
		url.addCommand(exitCmd);
		url.addCommand(streamCmd);
		url.addCommand(contentCmd);
		url.setCommandListener(this);
		msg.addCommand(exitCmd);
		msg.addCommand(backCmd);
		msg.setCommandListener(this);
	}
	protected void startApp(){
		display = Display.getDisplay(this);
		display.setCurrent(url);
	}
	protected void pauseApp(){}
	protected void destroyApp(boolean unconditional){}
	public void commandAction(Command c,Displayable d){
		if(c == exitCmd){
			destroyApp(false);
			notifyDestroyed();
		}else if(c == streamCmd){
			url.setTicker(tk);
			conType = 0;
			new Thread(this).start();
		}else if(c == contentCmd){
			url.setTicker(tk);
			conType = 1;
			new Thread(this).start();
		}else if(c == backCmd){
			url.setTicker(null);
			display.setCurrent(url);
		}
	}
	public void run(){
		try{
			switch(conType){
			case 0:
				getViaStreamConnection(url.getString());
				break;
			case 1:
				getViaContentConnection(url.getString());
				break;
			}
		}catch(Exception e){
			System.out.println("Error:"+e.getMessage());
		}
	}
	void getViaStreamConnection(String url) throws IOException{
		StringBuffer strbuf = new StringBuffer();
		StreamConnection c = null;
		InputStream is = null;
		try{
			c = (StreamConnection)Connector.open(url);
			is = c.openInputStream();
			int ch;
			while((ch = is.read())!= -1)
			{
				strbuf.append((char)ch);
			}
		}catch(Exception e){
			System.out.println("Error:"+e.getMessage());
		}finally{
			msg.setString(strbuf.toString());
			display.setCurrent(msg);
			strbuf = null;
			if(is != null) is.close();
			if(c != null) c.close();
		}
	}
	void getViaContentConnection(String url) throws IOException
	{
		StringBuffer strbuf = new StringBuffer();
		ContentConnection c = null;
		DataInputStream dis = null;
		String head = "";
		try{
			c = (ContentConnection)Connector.open(url);
			int len = (int)c.getLength();
			String encode = c.getEncoding();
			String type = c.getType();
			dis = c.openDataInputStream();
			if(len > 0){
				byte[] data = new byte[len];
				dis.readFully(data);
				head = "content-encoding:"+encode+"\n";
				head += "content-type:"+type+"\n";
				head += "Content-Length:"+len+"\n";
				for(int i=0;i<data.length;i++)
					strbuf.append((char)data[i]);
			}else{
				int ch;
				while((ch = dis.read())!=-1)
					strbuf.append((char)ch);
			}
		}catch(Exception e){
			System.out.println("Error:"+e.getMessage());
		}finally{
			msg.setString(head + strbuf.toString());
			display.setCurrent(msg);
			strbuf = null;
			if(dis != null) dis.close();
			if(c != null) c.close();
		}			
	}
}

⌨️ 快捷键说明

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