J2ME SOCKET
一、tomcat目录:http://blog.csdn.net/images/blog_csdn_net/yangtang_newton/image006.gif
将类放在:$tomcat\webapps\Root\web_inf\classes\
将HTML放在:$tomcat\webapps\Root\
对于自己的工程项目,可以在webapps下自己建立一个目录。比如
$tomcat\webapps\mywork\
那么自己的所有类需要放在:
$tomcat\webapps\mywork\web_inf\classes\下。对一些所有工程都共享的组件类,建议仍然放在$tomcat\webapps\Root\web_inf\classes\下
如果是resin:
html=>$resin\doc\
class=>$resin\doc\web_inf\classes\
servlets=>$resin\doc\web_inf\classes\
2、启动
1) 设置java_home
http://blog.csdn.net/images/blog_csdn_net/yangtang_newton/image008.jpg
2) 运行:
http://blog.csdn.net/images/blog_csdn_net/yangtang_newton/image010.jpg
3) 运行测试:
3、修改配置:
D:\jbuildre2005\thirdparty\jakarta-tomcat-5.0.27\conf\目录下
所有XML就是配置文件。比如要将启动的端口改动为:9000
那么,打开server.xml:
将port=”8080”改为9000即可
http://blog.csdn.net/images/blog_csdn_net/yangtang_newton/image018.jpg
4、MIDP中使用HTTP:
http://blog.csdn.net/images/blog_csdn_net/yangtang_newton/image020.jpghttp://blog.csdn.net/images/blog_csdn_net/yangtang_newton/image022.jpghttp://blog.csdn.net/images/blog_csdn_net/yangtang_newton/image024.jpg
1) 需要的头:
import java.io.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;2)主要方法://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); } 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); }
5、socket通信:
/* * Copyright 2003, 2004 Symbian Ltd. * For License terms see http://www.symbian.com/developer/techlib/codelicense.html */ import javax.microedition.io.*;import java.io.*; public class ClientConnection extends Thread { private final static String line1 = "002402^1000^120010000011111^"; private SocketMIDlet sM = null; private String url = null; private String request = null; public ClientConnection(SocketMIDlet sM) { this.sM = sM; //构造关于某个MIDlet的SOCKET实例 } public void sendMessage(String url) {//处理关于服务的连接字符串,//"socket://localhost:8900" this.url = url; request = line1;//请求的字符串 start(); } public void run() { try{ SocketConnection conn = (SocketConnection)Connector.open(url); DataOutputStream out = conn.openDataOutputStream(); byte[] buf= request.getBytes(); out.write(buf); out.flush(); out.close(); sM.textBox.insert(" Finished request!\nReceiving response...\n", sM.textBox.size()); DataInputStream in = conn.openDataInputStream(); int ch; while ( (ch = in.read()) != -1 && sM.textBox.size() < sM.textBox.getMaxSize()) { String str = new Character((char) ch).toString(); try { sM.textBox.insert(str, sM.textBox.size()); }catch(Exception e) { e.printStackTrace(); } } conn.close(); conn = null; }catch(Exception e){ e.printStackTrace(); } } }
可以在某个MIDLET中直接调用:
ClientConnection socketConn = new ClientConnection(this); socketConn.sendMessage(textBox.getString());
即可实现一次发送和接受
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yangtang_newton/archive/2006/03/28/640872.aspx
页:
[1]