📄 htppconn.java
字号:
import java.io.DataInputStream;import javax.microedition.io.Connector;import javax.microedition.io.HttpConnection;import javax.microedition.lcdui.*;import javax.microedition.lcdui.Image;import javax.microedition.midlet.MIDlet;/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author tpwhw */public class HtppConn extends Form implements CommandListener, Runnable { protected MIDlet mid; protected Display disp = null; protected Command cmd_Send = new Command("发送", Command.SCREEN, 1); protected Command cmd_Exit = new Command("结束", Command.EXIT, 1); protected Thread thread = null; private String[] Encode = {"ucs-2", "utf-8", "utf8", "utf_8", "gb2312", "gbk", "unicode"}; private Image[] img = new Image[Encode.length]; private ChoiceGroup chk_Encode = new ChoiceGroup("解码", Choice.EXCLUSIVE, Encode, img); private StringItem lbl_Value = new StringItem("返回值:", ""); private TextField txt_Param = new TextField("URLParam:", "参数中华人民共和国中国共产主义青年团中国共产党中国工商银行中华人民共和国ucs-2,再例如utf-8来说,utf-8、utf8和utf_8都会有可能。对于这一点,CLDC的规范中并没有给出严格的定义。所以在实际测试的过程中需要充分考虑到这个情况", 500, TextField.ANY); public HtppConn(MIDlet mid) { super("HTPP联接测试"); this.mid = mid; disp = Display.getDisplay(mid); disp.setCurrent(this); addCommand(cmd_Send); addCommand(cmd_Exit); setCommandListener(this); append(txt_Param); append(chk_Encode); append(lbl_Value); } public void commandAction(Command c, Displayable d) { if (c == cmd_Send) { removeCommand(c); thread = new Thread(this); thread.start(); } else if (c == cmd_Exit) { mid.notifyDestroyed(); } } public void run() { HttpConnection conn = null; try { //http://222.185.236.90/TEST1.asp String url="http://222.185.236.90/TEST1.asp?key="; String Param=URLEncoder.encode(txt_Param.getString(),"utf-8"); conn = (HttpConnection) Connector.open(url+Param); if (conn.getResponseCode() != HttpConnection.HTTP_OK) { lbl_Value.setText("错误代码:" + conn.getResponseMessage()); } DataInputStream dis = conn.openDataInputStream(); String en = chk_Encode.getString(chk_Encode.getSelectedIndex()); int len = (int) conn.getLength(); if (len == -1) { int c = 0; StringBuffer s = new StringBuffer(); while ((c = dis.read()) != -1) { s.append((char) c); } lbl_Value.setText(s.toString()); } else { byte[] b = new byte[len]; dis.read(b); String s = new String(b, en); lbl_Value.setText("返回内容:" + s); } } catch (Exception e) { lbl_Value.setText("出错:" + e.getMessage()); } finally { try { if (conn != null) { conn.close(); } } catch (Exception e) { } addCommand(cmd_Send); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -