如何应用J2ME平台中的通用联网框架开发联网的应用程序。

    联网的时候一定按照如下的流程做

    1、建立连接,设置传输方式推荐POST,设置方法头;
    2、打开输出流,传输数据给服务器;
    3、判断相应的状态码,进入不同流程控制,注意错误处理。如果OK则开始接受数据;
    4、关闭连接和流。

    下面是客户端的代码,对错误处理没有考虑太多。一共是5个class:

    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    public class HttpCommMIDlet extends MIDlet
    {
    private UIController uicontroller;  
    protected void startApp() throws MIDletStateChangeException
    {
    uicontroller = new UIController(this);
    uicontroller.init();     
    }  
    protected void pauseApp()
    {
    }  
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException
    {
    }
    }

    import java.io.IOException;

    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    public class UIController
    {

    private HttpCommMIDlet midlet;
    private InputCanvas inputUI;
    private DisplayCanvas displayUI;
    private Display display;
    private HttpCommHandler httpHandler;

    public UIController(HttpCommMIDlet midlet)
    {
    this.midlet = midlet;

    }

    public static class EventID
    {
    public static final int CONNECT_TO_SERVER = 0;
    public static final int DISPLAY_BACK_TO_INPUT = 1;
    }

    public void init()
    {
    display = Display.getDisplay(midlet);
    httpHandler = new HttpCommHandler(
    "http://yourip:8088/http/myservlet");
    inputUI = new InputCanvas(this);
    displayUI = new DisplayCanvas(this);
    display.setCurrent(inputUI);
    }

    public void setCurrent(Displayable disp)
    {
    display.setCurrent(disp);
    }

    public void handleEvent(int EventID, Object[] obj)
    {
    new EventHandler(EventID, obj).start();
    }

    private class EventHandler extends Thread
    {
    private int eventID;
    private Object[] obj;
    private Displayable backUI;

    public EventHandler(int eventID, Object[] obj)
    {
    this.eventID = eventID;
    this.obj = obj;
    }

    public void run()
    {
    synchronized (this)
    {
    run(eventID, obj);
    }
    }

    private void run(int eventID, Object[] obj)
    {
    switch (eventID)
    {
    case EventID.CONNECT_TO_SERVER:
    {
    try
    {

    String result = httpHandler
    .sendMessage((String) obj[0]);
    displayUI.init(result);
    setCurrent(displayUI);
    break;
    } catch (IOException e)
    {

    }
    }
    case EventID.DISPLAY_BACK_TO_INPUT:
    {
    setCurrent(inputUI);
    break;
    }
    default:
    break;
    }
    }
    };
    }


    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.StringItem;
    import javax.microedition.lcdui.TextField;
    public class InputCanvas extends Form implements CommandListener
    {
    private UIController uicontroller;
    private TextField inputField;
    private StringItem result;
    public static final Command okCommand = new Command("OK", Command.OK, 1);

    public InputCanvas(UIController uicontroller)
    {
    super("Http Comunication");
    this.uicontroller = uicontroller;
    inputField = new TextField("Input:", null, 20, TextField.ANY);
    this.append(inputField);
    this.addCommand(okCommand);
    this.setCommandListener(this);
    }

    public void commandAction(Command arg0, Displayable arg1)
    {

    if (arg0 == okCommand)
    {
    String input = inputField.getString();
    uicontroller.handleEvent(UIController.EventID.CONNECT_TO_SERVER,
    new Object[] { input });
    }
    }
    }

    import java.io.*;

    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    public class HttpCommHandler
    {
    private String URL;

    public HttpCommHandler(String URL)
    {
    this.URL = URL;
    }

    public String sendMessage(String message) throws IOException
    {
    HttpConnection httpConn;
    DataInputStream input;
    DataOutputStream output;
    String result;
    try
    {
    httpConn = open();
    output = this.openDataOutputStream(httpConn);
    output.writeUTF(message);
    output.close();
    input = this.openDataInputStream(httpConn);
    result = input.readUTF();
    closeConnection(httpConn,input,output);
    return result;
    }
    finally
    {
    }
    }

    public HttpConnection open() throws IOException
    {
    try
    {
    HttpConnection connection = (HttpConnection) Connector.open(URL);

    connection.setRequestProperty("User-Agent", System
    .getProperty("microedition.profiles"));
    connection.setRequestProperty("Content-Type",
    "application/octet-stream");
    connection.setRequestMethod(HttpConnection.POST);

    return connection;
    } catch (IOException ioe)
    {

    throw ioe;
    }

    }

    private DataInputStream openDataInputStream(HttpConnection conn)
    throws IOException

    {
    int code = conn.getResponseCode();
    if (code == HttpConnection.HTTP_OK)
    {
    return conn.openDataInputStream();
    } else
    {
    throw new IOException();
    }
    }

    private DataOutputStream openDataOutputStream(HttpConnection conn)
    throws IOException
    {
    return conn.openDataOutputStream();
    }
    private void closeConnection(HttpConnection conn, DataInputStream dis,
    DataOutputStream dos)
    {
    if(conn!= null)
    {
    try
    {
    conn.close();
    }
    catch(IOException e)
    {}
    }

    if(dis!=null)
    {
    try
    {
    dis.close();
    }
    catch(IOException e)
    {}
    }

    if(dos!=null)
    {
    try
    {
    dos.close();
    }
    catch(IOException e)
    {}
    }        
    }
    }


    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.StringItem;


    public class DisplayCanvas extends Form implements CommandListener
    {
    private UIController uicontroller;
    private StringItem result;
    private int index = 0;
    private boolean first = true;
    public static Command backCommand = new Command("Back", Command.BACK, 2);

    public DisplayCanvas(UIController uicontroller)
    {
    super("Result");
    this.uicontroller = uicontroller;
    result = new StringItem("you have input:", null);
    this.addCommand(backCommand);
    this.setCommandListener(this);

    }

    public void init(String message)
    {
    if (first)
    {
    result.setText(message);
    index = this.append(result);
    first = false;
    }
    else
    {
    this.delete(index);
    result.setText(message);
    this.append(result);
    }

    }


    public void commandAction(Command arg0, Displayable arg1)
    {
    uicontroller.handleEvent(UIController.EventID.DISPLAY_BACK_TO_INPUT,
    null);
    }
    }

    (责任编辑 火凤凰 sunsj@51cto.com  TEL:(010)68476636-8007)



    共3页: 上一页 [1] [2] 3
    【内容导航】
原文:开发J2ME联网应用程序
标 签:  J2ME  http  框架