Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>J2ME开发>>开发J2ME联网应用程序
开发J2ME联网应用程序
2007-04-17   来源:www.javaresearch.org  作者:未知

    尽管目前的无线网络不够理想,手机联网还是给我们开发人员不小的震撼的。毕竟这真的是件神奇的事情,不是吗?本文将讲述如何应用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)。

  --相关文章--
· 详细介绍手机游戏中的声音处理 (2007-04-17)
· 让JavaME程序实现真正Run Anywhere (2007-04-17)
· 端到端J2ME应用开发实例——介绍Smart Ticket (2007-04-17)
· 移动开发谁领风骚 J2ME开发工具面面观 (2007-04-17)
· 用NetBeans平台开发J2ME游戏实例讲解3 (2007-04-17)
· 用NetBeans平台开发J2ME游戏实例讲解2 (2007-04-17)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接