通过实例说明J2ME通用联网框架的使用

来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 16:02 点击:
尽管目前的无线网络不够理想,手机联网还是给我们开发人员不小的震撼的。毕竟这真的是件神奇的事情,不是吗?本文将讲述如何应用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 ;
   &nbs

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Java编程语言的八大优点
      • JVM对象生命周期详细介绍
      • Java平台上的CRM系统
      • Java 算数测试小程序
      • Command(命令模式)
      • Java 一个简单的画图程序
      • Java环境 使用Resin在NT环境下配置JSP环境
      • Java 日历的小程序
      • Java 统计代码的小工具
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1