j2ME 测试连网的例子

用户主界面:

/*
* HttpGetMidlet.java
*
* Created on 2006年11月15日, 下午9:38
*/


package  com.httpget;

import  javax.microedition.midlet. * ;
import  javax.microedition.lcdui. * ;

/**
*
@author  Administrator
@version
*/

public   class  HttpGetMidlet  extends  MIDlet  implements  CommandListener {
    
private Display display = null;
    
private Form mainForm = null;
    
private HttpThread http = null;
    
private TextField user = new TextField("用户名"""20, TextField.ANY);
    
private TextField password = new TextField("密 码"""20, TextField.PASSWORD);
    
private Command loginCommand = new Command("登陆", Command.OK, 1);
    
private Command exitCommand = new Command("退出", Command.EXIT, 2);
    
public void startApp() {
        
if(display == null){
            display 
= Display.getDisplay(this);
            mainForm 
= new Form("登陆界面");
            mainForm.append(user);
            mainForm.append(password);
            mainForm.addCommand(loginCommand);
            mainForm.addCommand(exitCommand);
            mainForm.setCommandListener(
this);
            http 
= new HttpThread(this);
            
new Thread(http).start();
            display.setCurrent(mainForm);
        }

        
else
            display.setCurrent(mainForm);
    }

    
    
public void pauseApp() {
        System.out.println(
"wait");
    }

    
    
public void destroyApp(boolean unconditional) {
    }

    
    
public void displayResult(int result){
        
if(result == 100){
            Form success 
= new Form("登陆成功");
            success.append(
"恭喜您,登陆成功");
            display.setCurrent(success);
        }

        
else{
            Alert alert 
= new Alert("错误");
            alert.setType(AlertType.ERROR);
            alert.setString(
"用户密码错误");
            alert.setTimeout(
2000);
            display.setCurrent(alert, mainForm);
            System.out.println(result);
        }

    }

    
    
public void exit(){
        destroyApp(
false);
        notifyDestroyed();
    }

    
    
public void commandAction(Command cmd, Displayable displayable){
        
if(cmd == loginCommand){
            String userName 
= user.getString();
            String pass 
= password.getString();
            String urlData 
= "username=" + userName + "&password=" + pass;
            http.setURLData(urlData);
            
synchronized(this){
                notify();
            }

        }

        
else if(cmd == exitCommand){
            exit();
        }

    }

}

线程:

/*
* HttpThread.java
*
* Created on 2006年11月15日, 下午9:57
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/


package  com.httpget;

import  java.io.DataInputStream;
import  java.io.IOException;
import  javax.microedition.io.Connector;
import  javax.microedition.io.HttpConnection;

/**
*
@author Administrator
*/

public   class  HttpThread  implements  Runnable {
    
private String urlData = "";
    
private HttpGetMidlet midlet= null;
    
private boolean done = false;
    
private static final String HOST = "http://localhost:8080";
    
    
/** Creates a new instance of HttpThread */
    
public HttpThread(HttpGetMidlet midlet) {
        
this.midlet = midlet;
    }

    
    
public void setURLData(String data){
        
this.urlData = data;
    }

    
    
public void stop(){
        done 
= true;
    }

    
    
public HttpConnection openConnection() throws IOException{
        HttpConnection conn 
= (HttpConnection)Connector.open(HOST + "/loginapps/login?" + urlData);
        
return conn;
    }

    
    
public void run(){
        
while(!done){
            
synchronized(midlet){
                
try{
                    midlet.wait();
                }
catch(InterruptedException e){
                    e.printStackTrace();
                }

                
if(!done){
                    
try{
                         HttpConnection conn 
= this.openConnection();
                         conn.setRequestMethod(HttpConnection.GET);
                         conn.setRequestProperty(
"User-Agent", System.getProperty("microedition.profiles"));
                         
int responseCode = conn.getResponseCode();
                         
if(responseCode != HttpConnection.HTTP_OK){System.out.println("unconnected");}
                            DataInputStream dis 
= conn.openDataInputStream();
                            
int result = dis.readInt();
                            midlet.displayResult(result);
                            dis.close();
                            conn.close();
                         
                    }
catch(IOException e){
                        e.printStackTrace();
                    }
catch(IllegalMonitorStateException ill){
                        ill.printStackTrace();
                    }

                   
                }

                
            }

        }

    }

    
    
}

服务器端程序,一个Servlet程序:

package  com.j2medev.ch6.http;

import  java.io. * ;
import  java.net. * ;

import  javax.servlet. * ;
import  javax.servlet.http. * ;

public   class  LoginServlet  extends  HttpServlet  {
    
    
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    
throws ServletException, IOException {
        request.setCharacterEncoding(
"UTF-8");
        java.util.Enumeration headers 
= request.getHeaderNames();
        System.out.println(
"HTTP头信息");
        
while(headers.hasMoreElements()){
            String header 
= (String)headers.nextElement();
            System.out.println(header
+"="+request.getHeader(header));
        }

        String userName 
= request.getParameter("username");
        String password 
= request.getParameter("password");
        System.out.println(userName
+":"+password);
        DataOutputStream dos 
= new DataOutputStream(response.getOutputStream());
        
if(userName.equals("詹建飞"&& password.equals("pass")){
             dos.writeInt(
100);
        }
else{
            dos.writeInt(
200);
        }

        dos.flush();
        dos.close();
    }


    
protected void doGet(HttpServletRequest request, HttpServletResponse response)
    
throws ServletException, IOException {
        processRequest(request, response);
    }

    
    
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    
throws ServletException, IOException {
        processRequest(request, response);
    }


    
public String getServletInfo() {
        
return "Short description";
    }

}

 

如下可能情况:

结果显示,要么是javax.microedition.io.ConnectionNotFoundException: TCP open
        at com.sun.midp.io.j2me.socket.Protocol.connect(+99)
        at com.sun.midp.io.ConnectionBaseAdapter.openPrim(+52)
        at com.sun.midp.io.j2me.socket.Protocol.openPrim(+108)
        at com.sun.midp.io.ConnectionBaseAdapter.openPrim(+14)
        at com.sun.midp.io.ConnectionBaseAdapter.openPrim(+8)
        at com.sun.midp.io.j2me.http.Protocol.connect(+73)
        at com.sun.midp.io.j2me.http.Protocol.streamConnect(+57)
        at com.sun.midp.io.j2me.http.Protocol.startRequest(+12)
        at com.sun.midp.io.j2me.http.Protocol.sendRequest(+38)
        at com.sun.midp.io.j2me.http.Protocol.sendRequest(+6)
        at com.sun.midp.io.j2me.http.Protocol.getResponseCode(+8)
        at com.httpget.HttpThread.run(+68)

要么就是unconnected,就是根本连不上,我用的是netbeans自带的tomcat5.5.7,如果我打开我机器上的tomcat5.5,就显示unconnected,如果不带开,就显示上面那段异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值