开发J2ME联网应用程序
尽管目前的无线网络不够理想,手机联网还是给我们开发人员不小的震撼的。毕竟这真的是件神奇的事情,不是吗?本文将讲述如何应用J2ME平台中的通用联网框架开发联网的应用程序。
%A 首先,必须说明一点:MIDP中规定,任何移动信息设备都必须提供通过http协议的支持,而像其他的通信方式例如socket是设备相关的。有些手机会支持,有些则不支持。这里只大概的说明一下http协议相关的内容,如果不了解这个方面的知识请参考http协议。在javax.microedition.io里面是大量的接口,只有一个connector类,当然在midp2.0里面添加了对push技术的支持,这个留做以后讲。connector类提供的最重要的方法是open()方法,它的返回值为Connection,你可以对他进行转换得到你需要的类型,比如我们以http协议访问服务器。
%A void postViaHttpConnection(String url) throws IOException {
%A HttpConnection c = null;
%A InputStream is = null;
%A OutputStream os = null;
%A int rc;
%A
%A try {
%A c = (HttpConnection)Connector.open(url);
%A
%A // Set the request method and headers
%A c.setRequestMethod(HttpConnection.POST);
%A c.setRequestProperty("If-Modified-Since",
%A "29 Oct 1999 19:43:31 GMT");
%A c.setRequestProperty("User-Agent",
%A "Profile/MIDP-2.0 Configuration/CLDC-1.0");
%A c.setRequestProperty("Content-Language", "en-US");
%A
%A // Getting the output stream may flush the headers
%A os = c.openOutputStream();
%A os.write("LIST games\n".getBytes());
%A os.flush(); // Optional, getResponseCode will flush
%A
%A // Getting the response code will open the connection,
%A // send the request, and read the HTTP response headers.
%A // The headers are stored until requested.
%A rc = c.getResponseCode();
%A if (rc != HttpConnection.HTTP_OK) {
%A throw new IOException("HTTP response code: " + rc);
%A }
%A
%A is = c.openInputStream();
%A
%A // Get the ContentType
%A String type = c.getType();
%A processType(type);
%A
%A // Get the length and process the data
%A int len = (int)c.getLength();
%A if (len > 0) {
%A int actual = 0;
%A int bytesread = 0 ;
%A byte[] data = new byte[len];
%A while ((bytesread != len) && (actual != -1)) {
%A actual = is.read(data, bytesread, len - bytesread);
%A bytesread += actual;
%A }
%A process(data);
%A } else {
%A int ch;
%A while ((ch = is.read()) != -1) {
%A process((byte)ch);
%A }
%A }
%A } catch (ClassCastException e) {
%A throw new IllegalArgumentException("Not an HTTP URL");
%A } finally {
%A if (is != null)
%A is.close();
%A if (os != null)
%A os.close();
%A if (c != null)
%A c.close();
%A }
%A }
%A 上面的代码是我取自API doc(建议多读一下api doc)。
%A
%A 下面根据自己的经验说明一下联网中比较重要的问题:
%A
%A 我们应该明白这是如何工作的,手机发送请求通过无线网络传输到运营商的WAP网关,WAP网关将请求转发到web服务器,服务器可以用cgi,asp,servlet/jsp等构建。服务器处理后会把响应转发到WAP网关,WAP网关再把它发送到手机上。WAP网关对我们开发人员来说是透明的我们不用管它。
%A 如果在你的联网程序上看不到Thread,Runnable这样的字眼,那么你的程序是不能运行的。因为考虑到网络的因素,为了避免操作堵塞。你必须把联网动作放到另外一个线程去运行,而不能在主线程运行。最好当联网的时候提供给用户一个等待的界面比如作一个动画界面。我下面提供的例子中没有用,因为我想把这个单独出来以后谈。
%A 通常联网的应用程序的界面是比较多的,最好我们使用MVC的模式来实现界面的导航。关于这个方面的说明我以前有文章讲述
%A 考虑好你想如何传递你数据,这一点是非常重要的。你可以用GET方法也可以使用POST方法,推荐后者。因为get方法只能是通过URL编码的传输。而POST更加灵活,配合DataInputStream、DataOutputStream来使用更是方便。必须清楚我们如何接受数据是跟数据如何发送过来相关的,例如在client端writeUTF(message);writeInt(4);writeBoolean(true),那么接受就应该readUTF();readInt();readBoolean();如果发送过来数据长度是可用的,那么我们可以建立一个适当的数组来接受,如果不可用我们就要一个适当容量的数组来接受。
%A 下面我提供一个实例来说明以上的问题,在应用程序中我们输入任意字符,通过连接server得到响应并显示出来。server我用servlet写的非常简单,只是在受到的内容后面加上“haha”,程序我安装到自己的手机上运行没有任何问题,就是GPRS网络不够快。Server是tomcat5实现的,关于如何部署servlet的问题超出了本文的讨论范围。我只提供代码,推荐用Eclipse+Lomboz开发j2ee程序。
%A
%A package com.north;
%A import java.io.DataInputStream;
%A import java.io.DataOutputStream;
%A import java.io.IOException;
%A import javax.servlet.ServletException;
%A import javax.servlet.http.HttpServlet;
%A import javax.servlet.http.HttpServletRequest;
%A import javax.servlet.http.HttpServletResponse;
%A
%A public class MyServlet extends HttpServlet {
%A protected void doGet(HttpServletRequest request,
%A HttpServletResponse response) throws ServletException, IOException {
%A DataInputStream dis = new DataInputStream(request.getInputStream());
%A String result = dis.readUTF();
%A DataOutputStream dos = new DataOutputStream(response.getOutputStream());
%A dos.writeUTF(result+"haha");
%A dos.close();
%A dis.close();
%A }
%A protected void doPost(HttpServletRequest request,
%A HttpServletResponse response) throws ServletException, IOException {
%A doGet(request,response);
%A }
%A }
%A 联网的时候一定按照如下的流程做
%A
%A 建立连接,设置传输方式推荐POST,设置方法头
%A 打开输出流,传输数据给服务器
%A 判断相应的状态码,进入不同流程控制,注意错误处理。如果OK则开始接受数据
%A 关闭连接和流
%A 下面是客户端的代码,对错误处理没有考虑太多。一共是5个class
%A import javax.microedition.midlet.MIDlet;
%A import javax.microedition.midlet.MIDletStateChangeException;
%A
%A
%A
%A public class HttpCommMIDlet extends MIDlet
%A {
%A
%A private UIController uicontroller;
%A
%A protected void startApp() throws MIDletStateChangeException
%A {
%A uicontroller = new UIController(this);
%A uicontroller.init();
%A
%A
%A }
%A
%A
%A protected void pauseApp()
%A {
%A }
%A
%A
%A protected void destroyApp(boolean arg0) throws MIDletStateChangeException
%A {
%A
%A
%A }
%A
%A }
%A
%A import java.io.IOException;
%A
%A import javax.microedition.lcdui.Display;
%A import javax.microedition.lcdui.Displayable;
%A public class UIController
%A {
%A
%A private HttpCommMIDlet midlet;
%A private InputCanvas inputUI;
%A private DisplayCanvas displayUI;
%A private Display display;
%A private HttpCommHandler httpHandler;
%A
%A public UIController(HttpCommMIDlet midlet)
%A {
%A this.midlet = midlet;
%A
%A }
%A
%A public static class EventID
%A {
%A public static final int CONNECT_TO_SERVER = 0;
%A public static final int DISPLAY_BACK_TO_INPUT = 1;
%A }
%A
%A public void init()
%A {
%A display = Display.getDisplay(midlet);
%A httpHandler = new HttpCommHandler(
%A "http://yourip:8088/http/myservlet");
%A inputUI = new InputCanvas(this);
%A displayUI = new DisplayCanvas(this);
%A display.setCurrent(inputUI);
%A }
%A
%A public void setCurrent(Displayable disp)
%A {
%A display.setCurrent(disp);
%A }
%A
%A public void handleEvent(int EventID, Object[] obj)
%A {
%A new EventHandler(EventID, obj).start();
%A }
%A
%A private class EventHandler extends Thread
%A {
%A private int eventID;
%A private Object[] obj;
%A private Displayable backUI;
%A
%A public EventHandler(int eventID, Object[] obj)
%A {
%A this.eventID = eventID;
%A this.obj = obj;
%A }
%A
%A public void run()
%A {
%A synchronized (this)
%A {
%A run(eventID, obj);
%A }
%A }
%A
%A private void run(int eventID, Object[] obj)
%A {
%A switch (eventID)
%A {
%A case EventID.CONNECT_TO_SERVER:
%A {
%A try
%A {
%A
%A String result = httpHandler
%A .sendMessage((String) obj[0]);
%A displayUI.init(result);
%A setCurrent(displayUI);
%A break;
%A } catch (IOException e)
%A {
%A
%A }
%A }
%A case EventID.DISPLAY_BACK_TO_INPUT:
%A {
%A setCurrent(inputUI);
%A break;
%A }
%A default:
%A break;
%A }
%A }
%A };
%A
%A }
%A
%A
%A import javax.microedition.lcdui.Command;
%A import javax.microedition.lcdui.CommandListener;
%A import javax.microedition.lcdui.Displayable;
%A import javax.microedition.lcdui.Form;
%A import javax.microedition.lcdui.StringItem;
%A import javax.microedition.lcdui.TextField;
%A
%A
%A public class InputCanvas extends Form implements CommandListener
%A {
%A private UIController uicontroller;
%A private TextField inputField;
%A private StringItem result;
%A public static final Command okCommand = new Command("OK", Command.OK, 1);
%A
%A public InputCanvas(UIController uicontroller)
%A {
%A super("Http Comunication");
%A this.uicontroller = uicontroller;
%A inputField = new TextField("Input:", null, 20, TextField.ANY);
%A this.append(inputField);
%A this.addCommand(okCommand);
%A this.setCommandListener(this);
%A }
%A
%A
%A public void commandAction(Command arg0, Displayable arg1)
%A {
%A
%A if (arg0 == okCommand)
%A {
%A String input = inputField.getString();
%A uicontroller.handleEvent(UIController.EventID.CONNECT_TO_SERVER,
%A new Object[] { input });
%A }
%A
%A }
%A
%A }
%A
%A
%A import java.io.*;
%A
%A import javax.microedition.io.Connector;
%A import javax.microedition.io.HttpConnection;
%A
%A
%A public class HttpCommHandler
%A {
%A private String URL;
%A
%A public HttpCommHandler(String URL)
%A {
%A this.URL = URL;
%A }
%A
%A public String sendMessage(String message) throws IOException
%A {
%A HttpConnection httpConn;
%A DataInputStream input;
%A DataOutputStream output;
%A String result;
%A try
%A {
%A httpConn = open();
%A output = this.openDataOutputStream(httpConn);
%A output.writeUTF(message);
%A output.close();
%A input = this.openDataInputStream(httpConn);
%A result = input.readUTF();
%A closeConnection(httpConn,input,output);
%A return result;
%A
%A }
%A
%A finally
%A {
%A
%A }
%A
%A }
%A
%A public HttpConnection open() throws IOException
%A {
%A try
%A {
%A HttpConnection connection = (HttpConnection) Connector.open(URL);
%A
%A connection.setRequestProperty("User-Agent", System
%A .getProperty("microedition.profiles"));
%A connection.setRequestProperty("Content-Type",
%A "application/octet-stream");
%A connection.setRequestMethod(HttpConnection.POST);
%A
%A return connection;
%A } catch (IOException ioe)
%A {
%A
%A throw ioe;
%A }
%A
%A }
%A
%A private DataInputStream openDataInputStream(HttpConnection conn)
%A throws IOException
%A
%A {
%A int code = conn.getResponseCode();
%A if (code == HttpConnection.HTTP_OK)
%A {
%A return conn.openDataInputStream();
%A } else
%A {
%A throw new IOException();
%A }
%A }
%A
%A private DataOutputStream openDataOutputStream(HttpConnection conn)
%A throws IOException
%A {
%A return conn.openDataOutputStream();
%A }
%A
%A private void closeConnection(HttpConnection conn, DataInputStream dis,
%A DataOutputStream dos)
%A {
%A if(conn!= null)
%A {
%A try
%A {
%A conn.close();
%A }
%A catch(IOException e)
%A {}
%A }
%A
%A if(dis!=null)
%A {
%A try
%A {
%A dis.close();
%A }
%A catch(IOException e)
%A {}
%A }
%A
%A if(dos!=null)
%A {
%A try
%A {
%A dos.close();
%A }
%A catch(IOException e)
%A {}
%A }
%A
%A }
%A
%A }
%A
%A
%A import javax.microedition.lcdui.Command;
%A import javax.microedition.lcdui.CommandListener;
%A import javax.microedition.lcdui.Displayable;
%A import javax.microedition.lcdui.Form;
%A import javax.microedition.lcdui.StringItem;
%A
%A
%A public class DisplayCanvas extends Form implements CommandListener
%A {
%A private UIController uicontroller;
%A private StringItem result;
%A private int index = 0;
%A private boolean first = true;
%A public static Command backCommand = new Command("Back", Command.BACK, 2);
%A
%A public DisplayCanvas(UIController uicontroller)
%A {
%A super("Result");
%A this.uicontroller = uicontroller;
%A result = new StringItem("you have input:", null);
%A this.addCommand(backCommand);
%A this.setCommandListener(this);
%A
%A }
%A
%A public void init(String message)
%A {
%A if (first)
%A {
%A result.setText(message);
%A index = this.append(result);
%A first = false;
%A }
%A else
%A {
%A this.delete(index);
%A result.setText(message);
%A this.append(result);
%A }
%A
%A }
%A
%A
%A public void commandAction(Command arg0, Displayable arg1)
%A {
%A uicontroller.handleEvent(UIController.EventID.DISPLAY_BACK_TO_INPUT,
%A null);
%A
%A }
%A
%A }
%A
%A
%A%A
%A
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。