📄 connecthttpmidlet.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class ConnectHttpMIDlet 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://tw.google.yahoo.com/bin/query_tw?p=MDIP&u=b&y=y&hc=0&hs=0";
private Form mainForm,resultForm;
private TextField URL;
private StringItem resultItem;
public ConnectHttpMIDlet() {
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,defaultURL,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 = 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;
String content = "";
try{
hpc = (HttpConnection)Connector.open(URLString);
content += "URL: " + hpc.getURL() + "\n";
content += "Protocol: " + hpc.getProtocol() + "\n";
content += "Host: " + hpc.getHost()+ "\n";
content += "Port: " + hpc.getPort()+ "\n";
content += "File: " + hpc.getFile()+ "\n";
content += "Query: " + hpc.getQuery()+ "\n";
content += "reference: " + hpc.getRef()+ "\n";
content += "request method: " + hpc.getRequestMethod();
}
catch(IOException e){
System.out.println("error");
}
try{
if(hpc != null)
hpc.close();
}
catch(IOException e2){
}
return content;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -