附录:
/*
*HttpMidlet.java
*/
importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;
importjavax.microedition.io.*;
importjava.io.*;
publicclassHttpMidletextendsMIDletimplementsCommandListener{;
//使用默认的URL。用户可以从图形用户接口改变这个值
privatestaticStringdefaultURL="http://localhost:8080/test/servlet/EchoServlet";;
//主MIDP显示
privateDisplaymyDisplay=null;
//输入URL的图形用户接口组件
privateformrequestScreen;
privateTextFieldrequestField;
//用于提交请求的图形用户接口组件
privateListlist;
privateString[]menuItems;
//用于显示服务器响应的图形用户接口组件
privateformresultScreen;
privateStringItemresultField;
//用于requestScreen的"send"按钮
CommandsendCommand;
//用于requestScreen的"exit"按钮
CommandexitCommand;
//用于requestScreen的"back"按钮
CommandbackCommand;
publicHttpMidlet(){;
//初始化图形用户接口组件
myDisplay=Display.getDisplay(this);
sendCommand=newCommand("SEND",Command.OK,1);
exitCommand=newCommand("EXIT",Command.OK,1);
backCommand=newCommand("BACK",Command.OK,1);
//显示请求的URL
requestScreen=newform("TypeinaURL:");
requestField=newTextField(null,defaultURL,100,TextField.URL);
requestScreen.append(requestField);
requestScreen.addCommand(sendCommand);
requestScreen.addCommand(exitCommand);
requestScreen.setCommandListener(this);
//选择想要的HTTP请求方法
menuItems=newString[]{;"GETRequest","POSTRequest"};;
list=newList("SelectanHTTPmethod:",List.IMPLICIT,menuItems,null);
list.setCommandListener(this);
//先是从服务器上收到的信息
resultScreen=newform("ServerResponse:");
resultScreen.addCommand(backCommand);
resultScreen.setCommandListener(this);
};//结束HttpMidlet()
publicvoidstartApp(){;
myDisplay.setCurrent(requestScreen);
};//结束startApp()
publicvoidcommandAction(Commandcom,Displayabledisp){;
//当用户点击"send"按钮
if(com==sendCommand){;
myDisplay.setCurrent(list);
};elseif(com==backCommand){;
requestField.setString(defaultURL);
myDisplay.setCurrent(requestScreen);
};elseif(com==exitCommand){;
destroyApp(true);
notifyDestroyed();
};//结束if(com==sendCommand)
if(disp==list&&com==List.SELECT_COMMAND){;
Stringresult;
if(list.getSelectedIndex()==0)//发送一个GET请求到服务器
result=sendHttpGet(requestField.getString());
else//发送一个POST请求到服务器
result=sendHttpPost(requestField.getString());
resultField=newStringItem(null,result);
resultScreen.append(resultField);
myDisplay.setCurrent(resultScreen);
};//结束if(dis==list&&com==List.SELECT_COMMAND)
};//结束commandAction(Command,Displayable)
privateStringsendHttpGet(Stringurl)
{;
HttpConnectionhcon=null;
DataInputStreamdis=null;
StringBufferresponseMessage=newStringBuffer();
try{;
//使用READ权限的标准的HttpConnection
hcon=(HttpConnection)Connector.open(url);
//从HttpConnection取得一个DataInputStream
dis=newDataInputStream(hcon.openInputStream());
//从服务器上取回响应
intch;
while((ch=dis.read())!=-1){;
responseMessage.append((char)ch);
};//结束while((ch=dis.read())!=-1)
};
catch(Exceptione)
{;
e.printStackTrace();
responseMessage.append("ERROR");
};finally{;
try{;
if(hcon!=null)hcon.close();
if(dis!=null)dis.close();
};catch(IOExceptionioe){;
ioe.printStackTrace();
};//结束try/catch
};//结束try/catch/finally
returnresponseMessage.toString();
};//结束sendHttpGet(String)
privateStringsendHttpPost(Stringurl)
{;
HttpConnectionhcon=null;
DataInputStreamdis=null;
DataOutputStreamdos=null;
StringBufferresponseMessage=newStringBuffer();
//请求体
Stringrequeststring="ThisisaPOST.";
try{;
//使用读写权限的HttpConnection
hcon=(HttpConnection)Connector.open(url,Connector.READ_WRITE);
//设置请求方法为POST
hcon.setRequestMethod(HttpConnection.POST);
//取得发送请求字符串的DataOutputStream
dos=hcon.openDataOutputStream();
byte[]request_body=requeststring.getBytes();
//发送请求字符串到服务器
for(inti=0;i
dos.writeByte(request_body[i]);
};//结束for(inti=0;i//取得做为接收服务器响应的DataInputStream
dis=newDataInputStream(hcon.openInputStream());
//从服务器上取回响应
intch;
while((ch=dis.read())!=-1){;
responseMessage.append((char)ch);
};//结束while((ch=dis.read())!=-1){;
};
catch(Exceptione)
{;
e.printStackTrace();
responseMessage.append("ERROR");
};
finally{;
//释放输入输出流和HTTP连接
try{;
if(hcon!=null)hcon.close();
if(dis!=null)dis.close();
if(dos!=null)dos.close();
};catch(IOExceptionioe){;
ioe.printStackTrace();
};//结束try/catch
};//结束try/catch/finally
returnresponseMessage.toString();
};//结束sendHttpPost(String)
publicvoidpauseApp(){;
};//结束pauseApp()
publicvoiddestroyApp(booleanunconditional){;
myDisplay=null;
requestScreen=null;
requestField=null;
resultScreen=null;
resultField=null;
};//结束destroyApp(boolean)
};//结束HttpMidlet