关于J2ME网络编程的程序,大家看看,急!!!!!!!(我就这点分了!)
这是我前两天在CSDN上面看到的一个例子,我自己做了一些修改。
我的手机端的程序如下:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class ClientApp extends MIDlet implements CommandListener{
Display display;
TextField tf1,tf2;
String tf1Str,tf2Str;
Form inputForm,returnForm;
Command cmdSend,cmdBack;
final static String defaultURL = "http://127.0.0.1:8080/servletapp";
public ClientApp(){
display = Display.getDisplay(this);
tf1 = new TextField("input first param:","卢东方",20,TextField.ANY);
tf2 = new TextField("input second param:","王桃群",20,TextField.ANY);
cmdSend = new Command("Send",Command.SCREEN,1);
cmdBack = new Command("Back",Command.SCREEN,1);
inputForm = new Form("pls input the param:");
inputForm.append(tf1);
inputForm.append(tf2);
inputForm.addCommand(cmdSend);
inputForm.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException{
display.setCurrent(inputForm);
}
public void invokeServlet(String url) throws IOException{
HttpConnection hc = null;
DataOutputStream dos = null;
DataInputStream dis = null;
StringBuffer msgBuffer = new StringBuffer();
try{
hc = (HttpConnection)Connector.open(url,Connector.READ_WRITE);
//设置请求属性为POST请求方式,默认的请求方式是GET
hc.setRequestMethod(HttpConnection.POST);
/**
//设置
hc.setRequestProperty("IF-Modified-Since","15 Oct 2003 08:47:14 GMT");
hc.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
hc.setRequestProperty("Content-Language","en-CA");
hc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
hc.setRequestProperty("Connection","Keep-Alive");
//Connection头可以控制MIDlet和Web服务器之间保持"keep alive"特色。
//"keep alive"特色是指在MIDlet和Web服务器间始终使用同一个HTTP连接来多次传递数据
//(在通常情况下,HTTP是无连接的协议,每次数据传输完毕后都将断开连接,
//而下次传递数据之前将重新建立连接)
**/
//发送请求参数到servl
dos = hc.openDataOutputStream();
byte[] request_body = tf1Str.getBytes();
for (int i = 0; i < request_body.length; i++) {
dos.writeByte(request_body[i]);
}
//dos.writeUTF(tf1Str);
//dos.writeUTF(tf2Str);
//用于发送请求参数给servlet
System.out.println("手机传递给servlet的第一个参数为:"+ tf1Str);
//主要起调试的作用,调试的结果将显示在WTK的控制台中
System.out.println("手机传递给servlet的第一个参数为:"+ tf2Str);
dos.flush();
dos.close();
//接收servlet响应数据
dis = new DataInputStream(hc.openInputStream());
int ch;
//首先检查Content-Length
long len = hc.getLength();
if(len != -1){
for (int i = 0;i < len;i++){
if((ch = dis.read()) != -1){
msgBuffer.append((char)ch);
}else{
//如果Content_Length无效
while ((ch = dis.read()) != -1){
msgBuffer.append((char)ch);
}
}
}
}
dis.close();
String return1Str = msgBuffer.toString();
//String return1Str = dis.readUTF();
//String return2Str = dis.readUTF();
System.out.println("手机接收到servlet端传来的第一个参数为:" + return1Str);
returnForm = new Form("返回的结果");
returnForm.append(return1Str);
returnForm.append("\n");
returnForm.addCommand(cmdBack);
returnForm.setCommandListener(this);
}catch(Exception e){
e.printStackTrace();
System.out.println("fuck!!!");
}finally{
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (hc != null) {
hc.close();
}
}
display.setCurrent(returnForm);
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
}
public void commandAction(Command c,Displayable d){
if(c == cmdBack){
display.setCurrent(inputForm);
}
if(c == cmdSend){
tf1Str = tf1.getString();
tf2Str = tf2.getString();
try{
invokeServlet(defaultURL);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
我的服务器端的程序如下:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletApp extends HttpServlet{
String tf1Str ;
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
//response.setContentType("text/html; charset=GBK");
//设置响应属性
//接收客户端的请求
ServletInputStream in = request.getInputStream();
DataInputStream din = new DataInputStream(in);
byte[] data = new byte[1000];
try{
tf1Str = din.readUTF();
}catch(Exception e){
e.printStackTrace();
tf1Str = "aa";
}
din.close();
//String tf2Str = dis.readUTF();
// 主要起调试的作用,调试的结果显示在tomcat的启动DOS窗口中
System.out.println("servlet端接收到手机传来的第一个参数为:" + tf1Str);
//System.out.println("servlet端接收到手机传来的第二个参数为:" + tf2Str);
//对接收的参数进行处理
String return1Str = tf1Str + ":早上好!" ;
//在接收到的参数后连接字符串
//String return2Str = tf2Str.concat(":晚上好!");
//发送处理后的参数给手机
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeUTF(return1Str);
//dos.writeUTF(return2Str);
byte[] byteresult=bout.toByteArray();
//发送相应头数据:
response.setContentType("application/octer-stream");
response.setContentLength(1);
response.setStatus(response.SC_OK);
OutputStream out = response.getOutputStream();
out.write(byteresult);
out.close();
System.out.println("servlet传递给手机的第一个参数为:" + return1Str);
//System.out.println("servlet传递给手机的第二个参数为:" + return2Str);
}
/*
public void doGett(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doGet(request,response);
}
*/
//清除资源
public void destroy(){
}
}
现在我运行手机端的程序,可以调用SERVLET了,可是SERVLET就是收不到手机端的数据,我用设置断点的方法试过了,然后我在服务器端又用数据发回到手机端,手机端仍然还是收不到,大家帮助我看看,问题可能在那里,我搞了一天了,眼睛都花了,帮帮忙,谢谢!!!!
问题点数:60、回复次数:6Top
1 楼ludf(lu)回复于 2003-12-02 15:52:23 得分 60
http://tech.ccidnet.com/pub/article/c1060_a70028_p1.html
文章是我和我男友写的
你加我QQ吧:3635751
有问题我们在QQ里讨论吧!
Top
2 楼liuyushen(寂微)回复于 2003-12-02 16:23:54 得分 0
不错,文章写的很详细,有指导意义。Top
3 楼sunny110(沙漠)回复于 2003-12-03 09:07:46 得分 0
学习Top
4 楼gdh_zj(永远和阳阳在一起:))回复于 2003-12-03 23:07:11 得分 0
你搞的太复杂了吧,需要设置内容长度吗??
我是直接 readUTF(STR)
writeUTF(STR)
不需要内容长度Top
5 楼gdh_zj(永远和阳阳在一起:))回复于 2003-12-03 23:08:24 得分 0
你换成JSP试试,服务器不需要设置内容类型Top
6 楼paulzhang(午夜晚风)回复于 2003-12-04 09:12:53 得分 0
好的我试过了!可以的,是我的错误!
现在我要传输图片(PNG),用readUTF(STR), writeUTF(STR)可行吗??或者还是用别的方法?请指教!!!!!谢谢……Top




