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

📄 simplehttpconnect.java

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

public class SimpleHttpConnect extends MIDlet implements CommandListener, Runnable{
	private Display display;
	private Command exitCmd,GetCmd,PostCmd;
	private Form f;
	private TextField url;
	private TextField name,password;
	private StringItem msg;
	private Ticker tk;
	private int method;
	public SimpleHttpConnect(){
		display = Display.getDisplay(this);
		f = new Form("HttpConnection");
		url = new TextField("URL:","http://localhost:8080/test/login.jsp",120,TextField.URL);
		name = new TextField("姓名:","John",120,TextField.ANY);
		password = new TextField("密码:","12345",120,TextField.PASSWORD);
		msg = new StringItem("内容:","");
		f.append(url);
		f.append(name);
		f.append(password);
		f.append(msg);
		tk = new Ticker("正在连接....");
		exitCmd = new Command("quit",Command.EXIT,1);
		GetCmd = new Command("Get",Command.SCREEN,1);
		PostCmd = new Command("Post",Command.SCREEN,1);
		f.addCommand(exitCmd);
		f.addCommand(GetCmd);
		f.addCommand(PostCmd);
		f.setCommandListener(this);
	}
	protected void startApp(){
		display.setCurrent(f);
	}
	protected void pauseApp(){	}
	protected void destroyApp(boolean unconditional){}
	public void commandAction(Command c,Displayable d){
		if(c == exitCmd)
		{
			destroyApp(false);
			notifyDestroyed();
		}else if(c == GetCmd){
			f.setTicker(tk);
			method = 0;
			new Thread(this).start();
		}else if(c == PostCmd){
			f.setTicker(tk);
			method = 1;
			new Thread(this).start();
		}
	}
	public void run(){
		try{
			switch(method){
			case 0:
				getViaHttpConnection(url.getString());
				break;
			case 1:
				postViaHttpConnection(url.getString());
				break;
			}
		}catch(Exception e){
			System.out.println("Error:"+e.getMessage());
		}
	}
	void getViaHttpConnection(String url) throws IOException{
		String message = "Name="+name.getString()+"&PSW="+password.getString();
		System.out.println(message);
		StringBuffer strbuf = new StringBuffer();
		HttpConnection c = null;
		DataInputStream dis = null;
		try{
			c = (HttpConnection)Connector.open(url+'?'+message);
			c.setRequestProperty("User-Agent", "Profile/MIDP-2.0");
			c.setRequestProperty("Accept", "text/plain,*/*");
			printInfo(c);
			int len = (int)c.getLength();
			dis = c.openDataInputStream();
			if(len > 0)
			{
				byte[] data = new byte[len];
				dis.readFully(data);
				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.setText(strbuf.toString());
			strbuf = null;
			if(dis != null) dis.close();
			if(c != null) c.close();
			f.setTicker(null);
		}
	}
	void postViaHttpConnection(String url) throws IOException{
		String message = "Name="+name.getString()+"&PSW="+password.getString();
		StringBuffer strbuf = new StringBuffer();
		HttpConnection c = null;
		DataInputStream dis = null;
		OutputStream os = null;
		try{
			c = (HttpConnection)Connector.open(url);
			c.setRequestMethod(HttpConnection.POST);
			c.setRequestProperty("Content-Language", "en-US");
			c.setRequestProperty("Content-Type", "application/x-www-forum-urlencoded");
			c.setRequestProperty("Content-Length", Integer.toString(message.length()));
			os = c.openOutputStream();
			os.write(message.getBytes());
			os.flush();
			printInfo(c);
			int len = (int)c.getLength();
			dis = c.openDataInputStream();
			if(len > 0){
				byte[] data = new byte[len];
				dis.read(data);
				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.setText(strbuf.toString());
			strbuf = null;
			if(dis != null) dis.close();
			if(c != null) c.close();
			f.setTicker(null);
		}
	}
	void printInfo(HttpConnection c) throws Exception{
		System.out.println("###### Http 连接信息 ######");
		System.out.println("URL="+c.getURL());
		System.out.println("Protocol="+c.getProtocol());
		System.out.println("Host="+c.getHost());
		System.out.println("Post="+c.getPort());
		System.out.println("File="+c.getFile());
		System.out.println("QueryString="+c.getQuery());
		int rc = c.getResponseCode();
		if(rc != HttpConnection.HTTP_OK){
			throw new IOException("HTTP response code:"+rc);
		}
		System.out.println("ResponseCode="+rc);
		System.out.println("ResponseMessage="+c.getResponseMessage());
		String key;
		for(int i=0;null!=(key = c.getHeaderFieldKey(i));i++)
		{
			System.out.println(key+"="+c.getHeaderField(i));
		}
	}
}

⌨️ 快捷键说明

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