📄 httppostmidlet.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class HttpPOSTMIDlet extends MIDlet
implements CommandListener {
private Command exitCommand;
private Command sendCommand;
private Command backCommand;
//The display for this MIDlet
private Display display;
// private String defaultURL = "http://localhost/JIDCA/hello2.asp";
private String defaultURL = "http://localhost/JIDCA//fetchNews.asp";
//private String defaultURL = "http://www.webappcabaret.com/jidca";
//private String defaultURL ="http://home.pchome.com.tw/online/jidca/index.html";
private Form mainForm,resultForm;
private TextField URL;
private StringItem resultItem;
public HttpPOSTMIDlet() {
display = Display.getDisplay(this);
exitCommand = new Command("离开",Command.EXIT,1);
sendCommand = new Command("送出",Command.OK, 1);
backCommand = new Command("上页",Command.OK,1);
}
// Start the MIDlet by creating the TextBox and
// associating the exit command and listener.
public void startApp() {
URL = new TextField(null,null,250,TextField.URL);
mainForm = new Form("请输入姓名");
mainForm.append(URL);
mainForm.addCommand(sendCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
// Pause is a no-op because there are no background
// activities or record stores to be closed.
public void pauseApp() { }
// Destroy must cleanup everything not handled
// by the garbage collector.
// In this case there is nothing to cleanup.
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable s) {
if(c==sendCommand){
String result="";
resultItem = new StringItem(null,result);
resultForm = new Form("结果");
String URLString = "name=" + URL.getString();
try{
result = requestUsingGET(URLString);
}
catch(IOException e){
result = "联机失败!";
}
resultItem.setText(result);
resultForm.append(resultItem);
resultForm.addCommand(backCommand);
resultForm.setCommandListener(this);
display.setCurrent(resultForm);
}
else if(c==backCommand){
URL.setString(defaultURL);
display.setCurrent(mainForm);
}
else{
destroyApp(false);
notifyDestroyed();
}
}
private String requestUsingGET(String URLString) throws IOException{
HttpConnection hpc = null;
DataInputStream dis = null;
DataOutputStream dos = null;
boolean newline = false;
String content = "";
try{
hpc = (HttpConnection)Connector.open(defaultURL);
hpc.setRequestMethod(HttpConnection.POST);
hpc.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
hpc.setRequestProperty("Content-Language","zh-tw");
hpc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
hpc.setRequestProperty("Content-Length",String.valueOf(URLString.length()));
dos = new DataOutputStream(hpc.openOutputStream());
dos.write(URLString.getBytes());
dos.flush();
// dis = new DataInputStream(hpc.openInputStream());
InputStreamReader xdis = new InputStreamReader(hpc.openInputStream());
// String qqq = dis.readUTF();
// System.out.println("qqq="+qqq);
/* while( xdis.read() != -1 ){
System.out.println("xx->" + (char)xdis.read());
}
byte[] bytes = new byte[255];
/* int xxx = dis.read(bytes);
System.out.println("bytes:" + xxx);
for(int i=0;i<xxx;i++)
System.out.println("x->" + (char)bytes[i]);
System.out.println("encoding " + System.getProperty("microedition.encoding"));
System.out.println("locale " + System.getProperty("microedition.locale"));
String xyz = new String(bytes,"big5");
System.out.println("xyz :"+xyz);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream xdos = new DataOutputStream(baos);
xdos.writeUTF(xyz);
bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
DataInputStream xis = new DataInputStream(bais);
System.out.println("utf:" + xis.readUTF());
System.out.println("bytes:" + String.valueOf(bytes));*/
int character;
// char xcontent = dis.readChar();
//System.out.println("content : "+xcontent);
while((character = xdis.read()) != -1 ){
if((char)character =='\\'){
newline = true;
continue;
}
else {
if((char)character =='n' && newline){
content += "\n";
newline = false;
}
else if(newline){
content += "\\" +(char)character ;
newline = false;
}
else {
content += (char)character;
newline = false;
}
}
}//*/
}
catch(IOException e){
System.out.println("error:"+e);
}
try{
if(hpc != null)
hpc.close();
if(dis != null)
dis.close();
}
catch(IOException e2){
}
return content;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -