用户主界面:
/**/
/* * 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,如果不带开,就显示上面那段异常