package hello;
import Javax.microedition.midlet.*;
import Javax.microedition.lcdui.*;
import Javax.microedition.io.*;
import Java.io.*;
public class Midlet extends MIDlet{
private Display display;
public Midlet(){
display = Display.getDisplay(this);
}
public void startApp(){
try{
String uURL ="http://3g.pp.cn/phone/app/gpcx.JSP?sid=1182917775043&dirid=03311317&gpdm=600156";
StringBuffer responseMessage = new StringBuffer();
HttpConnection hc = (HttpConnection)Connector.open(uURL, Connector.READ_WRITE);
hc.setRequestMethod(HttpConnection.POST);
DataOutputStream DOS = hc.openDataOutputStream();
DataInputStream dis = new DataInputStream(hc.openInputStream());
//-------------------------關鍵代碼--------------------------------
byte []str=new byte[1000];//從內存申請空間
dis.read(str);//把讀取返回的信息保存在str中
String content= BytesToString(str);//把str轉換為字符串
//-------------------------關鍵代碼--------------------------------
Form f = new Form("HTTP Test");
f.append(content);
display.setCurrent(f);
}catch(Exception e){
}
}
//-----------------------關鍵函數-----------------------------
public String BytesToString(byte[] rec) { //從字節讀取內容
ByteArrayInputStream bais = new ByteArrayInputStream(rec);
DataInputStream dis = new DataInputStream(bais);
String BTS=null;
try {
BTS=new String(rec,"UTF-8");
bais.close();
dis.close();
} catch (Exception e) {
e.printStackTrace();
}
return BTS;
}
//-----------------------關鍵函數-----------------------------
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
}
}