📄 httptest.java
字号:
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HTTPTest extends MIDlet implements CommandListener,Runnable{
private Display display;
private Command back = new Command("Back",Command.BACK,0) ;
private String urlStrPost = "http://localhost:8080/myexample/index.html";
private String urlStrGet = "http://localhost:8080/myexample/index.html";
private boolean beGet=false;
public HTTPTest(){
display = Display.getDisplay(this);
}
public void startApp() {
MainForm();
}
public void MainForm()
{
//请求方法选择列表
List myList = new List("Choose HTTP Request Method", Choice.IMPLICIT) ;
myList.append("POST", null) ; //POST方法
myList.append("GET", null) ; //GET方法
myList.setCommandListener(this);
display.setCurrent(myList) ;
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
}
//使用Get方法
public void HTTPGet(String urlStrGet) throws IOException {
HttpConnection hc = null; //HttpConnection连接
InputStream is = null; //用于读入数据
StringBuffer buf = new StringBuffer(); //用于保存服务器的响应数据
TextBox t = null;
try{
//打开一个HttpConnection连接
hc = (HttpConnection)Connector.open(urlStrGet);
//设置请求方法
hc.setRequestMethod(HttpConnection.GET);
//设置请求属性
hc.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
is = hc.openDataInputStream();
int ch;
//接收服务器的响应数据
while((ch = is.read()) != -1) {
buf.append((char) ch);
}
t = new TextBox("Get Test Page", buf.toString(), 1024, 0);
t.addCommand(back);
t.setCommandListener(this);
}finally {
is.close();
hc.close();
}
display.setCurrent(t);
}
//使用POST方法
public void HTTPPost(String urlStrPost) throws IOException {
HttpConnection hc = null; //HttpConnection连接
InputStream is = null; //用于读入数据
OutputStream os = null; //用于发出请求
StringBuffer buf = new StringBuffer(); //用于保存服务器的响应数据
TextBox t = null;
try{
//打开一个HttpConnection连接
hc = (HttpConnection) Connector.open(urlStrPost);
//设置请求方法
hc.setRequestMethod(HttpConnection.POST);
//设置请求属性
hc.setRequestProperty("CONTENT-TYPE","application/x-www-form-Agent");
hc.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
hc.setRequestProperty("Content-Language","en-CA");
os = hc.openOutputStream();
String str = "key=value";
byte postmsg[] = str.getBytes();
//发送请求
for(int i = 0; i < postmsg.length; i++) {
os.write(postmsg[i]);
}
os.flush();
is = hc.openDataInputStream();
int ch;
//接收服务器的响应数据
while((ch = is.read()) != -1) {
buf.append((char)ch);
}
t = new TextBox("Post Test Page", buf.toString(), 1024, 0);
t.addCommand(back);
t.setCommandListener(this);
}finally{
is.close();
os.close();
hc.close();
}
display.setCurrent(t);
}
public void commandAction(Command c,Displayable s)
{
//主屏幕的菜单选择
if(c == List.SELECT_COMMAND ) {
List tmp = (List) s ;
//确定选择了哪一种方法
switch(tmp.getSelectedIndex()){
case 0 : beGet=false; //选择了POST方法
break ;
case 1 : beGet=true; //选择了GET方法
break ;
}
Thread t=new Thread(this); //启动线程
t.start();
}
//返回选择界面
if(c.getLabel().equals("Back")) {
MainForm() ;
}
}
public void run(){
try{
if(beGet==true) //调用GET请求方法
HTTPGet(urlStrGet) ;
else
HTTPPost(urlStrPost) ; //调用POST请求方法
}catch(Exception e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -