J2ME SOCKET

一、tomcat目录:

将类放在:$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

2)      运行:

3)      运行测试:

3、修改配置:

D:/jbuildre2005/thirdparty/jakarta-tomcat-5.0.27/conf/目录下

所有XML就是配置文件。比如要将启动的端口改动为:9000

那么,打开server.xml:

port=”8080”改为9000即可

 

4MIDP中使用HTTP

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[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);

       }

5socket通信:

  

/*

 * 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; //构造关于某个MIDletSOCKET实例

    }

   

    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());

即可实现一次发送和接受

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值