电子工程师的网站
首 页 |  新闻资讯 | 最新产品 | 解决方案 | 技术参数
设计应用
电路图 | 技术资料 | 芯片资料 | 技术论坛
  现在位置: 首页 > 设计应用 > 嵌入式系统/ARM技术 > 详细信息
嵌入式系统/ARM技术:开发J2ME联网应用程序
来源:   时间: 2008-1-30 6:24:09    
尽管目前的无线网络不够理想,手机联网还是给我们开发人员不小的震撼的。毕竟这真的是件神奇的事情,不是吗?本文将讲述如何应用J2ME平台中的通用联网框架开发联网的应用程序。

     首先,必须说明一点:MIDP中规定,任何移动信息设备都必须提供通过http协议的支持,而像其他的通信方式例如socket是设备相关的。有些手机会支持,有些则不支持。这里只大概的说明一下http协议相关的内容,如果不了解这个方面的知识请参考http协议。在javax.microedition.io里面是大量的接口,只有一个connector类,当然在midp2.0里面添加了对push技术的支持,这个留做以后讲。connector类提供的最重要的方法是open()方法,它的返回值为Connection,你可以对他进行转换得到你需要的类型,比如我们以http协议访问服务器。
void postViaHttpConnection(String url) throws IOException {
        HttpConnection c = null;
        InputStream is = null;
        OutputStream os = null;
        int rc;

        try {
            c = (HttpConnection)Connector.open(url);

            // Set the request method and headers
            c.setRequestMethod(HttpConnection.POST);
            c.setRequestProperty("If-Modified-Since",
                "29 Oct 1999 19:43:31 GMT");
            c.setRequestProperty("User-Agent",
                "Profile/MIDP-2.0 Configuration/CLDC-1.0");
            c.setRequestProperty("Content-Language", "en-US");

            // Getting the output stream may flush the headers
            os = c.openOutputStream();
            os.write("LIST games\n".getBytes());
            os.flush();           // Optional, getResponseCode will flush

            // Getting the response code will open the connection,
            // send the request, and read the HTTP response headers.
            // The headers are stored until requested.
            rc = c.getResponseCode();
            if (rc != HttpConnection.HTTP_OK) {
                throw new IOException("HTTP response code: " + rc);
            }

            is = c.openInputStream();

            // Get the ContentType
            String type = c.getType();
            processType(type);

            // Get the length and process the data
            int len = (int)c.getLength();
            if (len > 0) {
                 int actual = 0;
                 int bytesread = 0 ;
                 byte[] data = new byte[len];
                 while ((bytesread != len) && (actual != -1)) {
                    actual = is.read(data, bytesread, len - bytesread);
                    bytesread += actual;
                 }
                process(data);
            } else {
                int ch;
                while ((ch = is.read()) != -1) {
                    process((byte)ch);
                }
            }
        } catch (ClassCastException e) {
            throw new IllegalArgumentException("Not an HTTP URL");
        } finally {
            if (is != null)
                is.close();
            if (os != null)
                os.close();
            if (c != null)
                c.close();
        }
    }
上面的代码是我取自API doc(建议多读一下api doc)。

下面根据自己的经验说明一下联网中比较重要的问题:

  1. 我们应该明白这是如何工作的,手机发送请求通过无线网络传输到运营商的WAP网关,WAP网关将请求转发到web服务器,服务器可以用cgi,asp,servlet/jsp等构建。服务器处理后会把响应转发到WAP网关,WAP网关再把它发送到手机上。WAP网关对我们开发人员来说是透明的我们不用管它。
  2. 如果在你的联网程序上看不到Thread,Runnable这样的字眼,那么你的程序是不能运行的。因为考虑到网络的因素,为了避免操作堵塞。你必须把联网动作放到另外一个线程去运行,而不能在主线程运行。最好当联网的时候提供给用户一个等待的界面比如作一个动画界面。我下面提供的例子中没有用,因为我想把这个单独出来以后谈。
  3. 通常联网的应用程序的界面是比较多的,最好我们使用MVC的模式来实现界面的导航。关于这个方面的说明我以前有文章讲述
  4. 考虑好你想如何传递你数据,这一点是非常重要的。你可以用GET方法也可以使用POST方法,推荐后者。因为get方法只能是通过URL编码的传输。而POST更加灵活,配合DataInputStream、DataOutputStream来使用更是方便。必须清楚我们如何接受数据是跟数据如何发送过来相关的,例如在client端writeUTF(message);writeInt(4);writeBoolean(true),那么接受就应该readUTF();readInt();readBoolean();如果发送过来数据长度是可用的,那么我们可以建立一个适当的数组来接受,如果不可用我们就要一个适当容量的数组来接受。

下面我提供一个实例来说明以上的问题,在应用程序中我们输入任意字符,通过连接server得到响应并显示出来。server我用servlet写的非常简单,只是在受到的内容后面加上“haha”,程序我安装到自己的手机上运行没有任何问题,就是GPRS网络不够快。Server是tomcat5实现的,关于如何部署servlet的问题超出了本文的讨论范围。我只提供代码,推荐用Eclipse+Lomboz开发j2ee程序。

package com.north;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  DataInputStream dis = new DataInputStream(request.getInputStream());
  String result = dis.readUTF();
  DataOutputStream dos = new DataOutputStream(response.getOutputStream());
  dos.writeUTF(result+"haha");

  dos.close();
   dis.close();
   }
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  doGet(request,response);
   }
}
    联网的时候一定按照如下的流程做



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


    }


}

相关信息
发表评论
打印本页 关闭本页
泰克推出业内首个手持式RF干扰查找工具
    泰克公司日前宣布,在其NetTek系列中增加最新产品-RF Scout干扰查找器。据称,这一产品在一个坚固耐用的手持式平台中为查找RF干扰、检查信号质量提供了所有必要的工具,使得网络性能规划和优化团队能够在随时可以用于现场的工具中,探索、发现、分析和存档干扰或其它信号质量问题。   转向速度更高的无线数据业务的发展趋势,正推动着对更干净的RF信号的需求,因为高速数据速率依...
>>详细内容
高性能射频调制器促成多载波通信发送器设计(上)
    蜂窝发送器的设计依赖于能够保持高线性度和高动态范围的高性能RF调制器。随着多载波发送器的增长,RF调制器必须保持低噪声基底,从而提供较高的性能指标,这通常取决于二阶或三阶互调。本文讨论了这些需求,并说明MAX2022能够满足典型四载波WCDMA发送架构的要求。 概述   现有的蜂窝基站大多采用超外差结构发送或接收射频信号。这种结构需要两次变频或更多的上、下变频级、中间滤...
>>详细内容
高性能射频调制器促成多载波通信发送器设计(下)
    蜂窝发送器的设计依赖于能够保持高线性度和高动态范围的高性能RF调制器。随着多载波发送器的增长,RF调制器必须保持低噪声基底,从而提供较高的性能指标,这通常取决于二阶或三阶互调。本文讨论了这些需求,并说明MAX2022能够满足典型四载波WCDMA发送架构的要求。 工作在UMTS波段的多载波WCDMA   多项性能指标综合体现出的优势在于多载波的互调特性。从本文可以看出,MAX2022能够...
>>详细内容
CSR蓝牙技术帮助诺基亚推出高性能蓝牙耳机
    目前已经上市的新款诺基亚蓝牙耳机BH-200采用了CSR公司的BlueCore3-Audio Flash (BlueCore3-AF),其语音通讯完全符合蓝牙v1.2技术规范。诺基亚蓝牙耳机BH-200是采用HFP1.5免提协议的第一款耳机,也是诺基亚公司采用CSR BlueCore技术并且已经上市的高端无线耳机系列之一。   诺基亚蓝牙耳机BH-200仅重14克,尺寸为49x22x21毫米,支持5小时30分钟通话时间及150小时待机时间。该款耳...
>>详细内容
PC-ISDN的接口设计
    摘要:提出了PC与基本速率ISDN连接的软硬件设计方案。介绍了基于PC总线、80C188微处理器、Mitel公司的ISDN通信器件以及IDT公司的EPLD逻辑硬件的ISDN卡的硬件结构,同时对软件的结构、任务和模块以及实现方案也作了说明。   关键词:PC-ISDN   ISDN 数据通信 Intel 80C188   1 系统设计考虑  计算机的普及及促进了远程通信的发展。然而1M字节大小的文件经过目前最快的V.34  ...
>>详细内容
德州仪器 Aureus™ 数字音频解决方案
  德州仪器Aureus™数字音频解决方案助力哈曼卡顿、安桥与雅马哈家庭娱乐新品获奖的音频DSP帮助OEM厂商为客户实现精彩特性。   日前,德州仪器(TI)宣布三家领先的音视频(A/V)接收机制造商(哈曼卡顿、安桥与雅马哈)已采用获奖的Aureus™系列高性能音频DSP产品,并应用在多款新一代A/V接收机与数字媒体中心产品中,从而为创新型家庭娱乐应用带来质量出众的音频体验。   TI高性...
>>详细内容
已有(
)位对此新闻感兴趣的网发发表了看法 >>更多评论
内 容:
     
 
热点新闻
一周排行
关于我们 | 服务项目 | 付款方式 | 广告服务 | 联系我们 | 友情链接 | 投诉 建议 合作 | 网站地图 | 加入收藏
Copyright © 2007-2008 WEEQOO.COM Corp.All Rights Reserved. 版权所有 经营许可证编号:浙B2-20050339 法律声明
维库电子旗下网站:维库电子市场网 | ChinaICMart | 维库电子开发网 | 维库电子人才网
总部:杭州市下城区朝晖路182号国都发展大厦1号楼80A
电话:0571-85889139-8007 QQ:303939539 | MSN:zh1226@hotmail.com |  邮箱:laz8258@163.com dzsc51@163.com