文谷首页 | 业界传真 | 网络技术 | 服务器 | 数据库 | 存储技术 | 系统安全 | 无线技术 | Cisco | .Net | Windows | Linux | Unix | Java
电子商务 | 网站工程 | 网页设计 | 平面设计 | 多媒体 | 编程语言 | Oracle | MSSQL | Photoshop | ASP | PHP | 实用技巧 | 进程查询 | 文谷论坛
Java频道
 资讯动态   考试认证   新手入门   核心技术   高级技术   J2EE   J2ME   XML   开源技术   其他技术
您现在的位置: IT文谷 >> 开发平台 >> Java >> J2ME >> 核心技术 >> 文章正文
使用J2ME中的page进行编码转化使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化
使用J2ME中的page进行编码转化
使用J2ME中的page进行编码转化使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化
使用J2ME中的page进行编码转化使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化
一、摘要

  根据xinshouj2me在j2me版提出的“httpconnection网络连接的问题”,本人分析了一下:由于www.163.com上的页面编码为gb2312,所以使用utf8读取由于编码方式不同会得到乱码。于是本人根据page的编码灵活进行编码转化,在此与大家共享、讨论。

二、代码分析

1.HttpConnectionHandler接口类
  最好根据page的编码灵活进行编码转化,于是本人定义了一个HttpConnectionHandler接口类:

HttpConnectionHandler.java
package com.bjinfotech.practice.j2me.httpConnection;

import java.util.Hashtable;
import javax.microedition.io.HttpConnection;

/**
* Http连接处理器接口
* @author cleverpig
*
*/
public interface HttpConnectionHandler {
        //http请求常量
        public static final String RQH_HOST="X-Online-Host";
        public static final String RQH_ACCEPT="Accept";
        public static final String RQH_CONTENT_LANGUAGE="Content-Language";
        public static final String RQH_CONTENT_TYPE="Content-Type";
        public static final String RQH_CONNECTION_OPTION="Connection";
        //http回应常量
        public static final String RSH_DATE="Date";
        public static final String RSH_SERVER="Server";
        public static final String RSH_MODIFIEDDATE="Last-Modified";
        public static final String RSH_CONTENT_ENCODING="Content-Encoding";
        public static final String RSH_CONTENT_LENGTH="Content-Length";
        public static final String RSH_CONTENT_TYPE="Content-Type";
        
        public boolean putRequestHeaderProperty(
                        HttpConnection conn,
                        String key,
                        String keyValue);
        
        public String getResponseHeaderProperty(
                        HttpConnection conn,
                        String key);
        
        public boolean setRequestMethod(
                        HttpConnection conn,
                        String methodName);
        
        public boolean putRequestHeader(
                        HttpConnection conn,
                        Hashtable propertiesPair);
        
        public Hashtable getResponseHeader(
                        HttpConnection conn,
                        String[] propertyNames);
        
        public boolean sendRequestData(
                        HttpConnection conn,
                        Object sendData);
        
        public Object getResponseData(HttpConnection conn);
        
}

2.HTML_HttpConnectionHandlerImpl类

  根据HttpConnectionHandler接口规范实现了该接口——HTML_HttpConnectionHandlerImpl类。

HTML_HttpConnectionHandlerImpl.java
package com.bjinfotech.practice.j2me.httpConnection;

import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import javax.microedition.io.HttpConnection;

/**
* http连接处理器实现
* @author cleverpig
*
*/
public class HTML_HttpConnectionHandlerImpl implements HttpConnectionHandler{
        private String message="";
        
        public HTML_HttpConnectionHandlerImpl(){
        }
        
        public boolean putRequestHeaderProperty(
                        HttpConnection conn,
                        String key,
                        String keyValue){
                message="";
                try{
                        conn.setRequestProperty(key,keyValue);
                        return true;
                }
                catch(Exception ex){
                        message=ex.getMessage();
                }
                return false;
        }
        
        public String getResponseHeaderProperty(
                        HttpConnection conn,
                        String key){
                return conn.getRequestProperty(key);
        }
        
        public boolean putRequestHeader(
                        HttpConnection conn,
                        Hashtable propertiesPair){
                Enumeration keyEnumer=propertiesPair.keys();
                boolean result=true;
                while(keyEnumer.hasMoreElements()){
                        String keyName=(String)keyEnumer.nextElement();
                        String keyValue=(String)propertiesPair.get(keyName);
                        if (putRequestHeaderProperty(conn,keyName,keyValue)==false){
                                result=false;
                        }
                }
                return result;
        }
        
        public boolean setRequestMethod(
                        HttpConnection conn,
                        String methodName){
                message="";
                try{
                        conn.setRequestMethod(methodName);
                        return true;
                }
                catch(Exception ex){
                        message=ex.getMessage();
                        return false;
                }
        }
        
        public Hashtable getResponseHeader(
                        HttpConnection conn,
                        String[] propertyNames){
                Hashtable result=new Hashtable();
                for(int i=0;i<propertyNames.length;i++){
                        String keyValue=conn.getRequestProperty(propertyNames[i]);
                        result.put(propertyNames[i],keyValue);
                }
                return result;
        }
        
        public boolean sendRequestData(
                        HttpConnection conn,
                        Object sendData){
                message="";
                try{
                        DataOutputStream os=conn.openDataOutputStream();
                        os.writeUTF((String)(sendData));
                        os.flush();
                        return true;
                }
                catch(Exception ex){
                        message=ex.getMessage();
                        return false;
                }
        }
        
        public Object getResponseData(HttpConnection conn){
                DataInputStream is=null;
                message="";
                try{
                        is=conn.openDataInputStream();
                }
                catch(Exception ex){
                        message=ex.getMessage();
                        return null;
                }
                
                byte[] data=null;
                String type=getResponseHeaderProperty(conn,RSH_CONTENT_TYPE);
                int len = 0;
                try{
                        len=Integer.parseInt(getResponseHeaderProperty(conn,RSH_CONTENT_LENGTH));
                }
                catch(Exception ex){
                        len=0;
                }
                if (len > 0) {
             int actual = 0;
             int bytesread = 0 ;
             data = new byte[len];
             while ((bytesread != len) && (actual != -1)) {
                     try{
                             actual = is.read(data, bytesread, len - bytesread);
                             bytesread += actual;
                     }
                     catch(Exception ex){
                             message=ex.getMessage();
                             return null;
                     }
             }
        } else {
            int ch;
            Vector vbuffer=new Vector();
            try{
                    while ((ch = is.read()) != -1) {
                            vbuffer.addElement(new Integer(ch));
                    }
            }
                       catch(Exception ex){
                                message=ex.getMessage();
                                return null;
                       }
            len=vbuffer.size();
            data = new byte[len];
            for(int i=0;i<len;i++){
                    data[i]=((Integer)vbuffer.elementAt(i)).byteValue();
            }
        }
        
        String result=new String(data);
        int flagBeginPosition=result.indexOf("charset=");
        int flagEndPosition=result.indexOf("\">",flagBeginPosition);
        if (flagEndPosition>flagBeginPosition){
                type=result.substring(flagBeginPosition+"charset=".length(),flagEndPosition);
        }
        System.out.println("获得html字符集:"+type);
        if (type!=null){
                
                try{
                        result=new String(data,type);
                }
                catch(Exception ex){
                        message=ex.getMessage();
                }
        }
        return result;
        }
        
        public String getMessage(){
                return message;
        }
        
        
}
上面实现类中根据page中的实际编码类型对html字符串进行了编码转化,这样就实现了page编码的灵活转化。
虽然灵活性加强了,但是对于内存的占用也随之增加了一倍。

三.测试代码

package com.bjinfotech.practice.j2me.httpConnection;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
import java.util.Hashtable;

public class HttpConnectionMIDlet extends MIDlet {

        public HttpConnectionMIDlet() {
                super();
        }

        
        protected void startApp() throws MIDletStateChangeException {
                HTML_HttpConnectionHandlerImpl handler=new HTML_HttpConnectionHandlerImpl();
                try{
                        String host="10.11.3.99";
                        String url="http://10.11.3.99:8080/test_gbk.html";
//                        String url="http://10.11.3.99:8080/test_gb2312.html";
//                        String url="http://10.11.3.99:8080/test_utf8.html";
                        
                        HttpConnection conn=(HttpConnection)Connector.open(url);
                        if (conn.getResponseCode()==HttpConnection.HTTP_OK){
                                System.out.println("建立连接成功");
                                
                                Hashtable requestHeaderPair=new Hashtable();
                                requestHeaderPair.put(
                                                HTML_HttpConnectionHandlerImpl.RQH_HOST,
                                                host);
                                requestHeaderPair.put(
                                                HTML_HttpConnectionHandlerImpl.RQH_CONTENT_TYPE,
                                                "application/octet-stream");
                                requestHeaderPair.put(
                                                HTML_HttpConnectionHandlerImpl.RQH_CONTENT_LANGUAGE,
                                                "en-US");
                                requestHeaderPair.put(
                                                HTML_HttpConnectionHandlerImpl.RQH_ACCEPT,
                                                "application/octet-stream");
                                requestHeaderPair.put(
                                                HTML_HttpConnectionHandlerImpl.RQH_CONNECTION_OPTION,
                                                "Keep-Alive");
                                handler.putRequestHeader(conn,requestHeaderPair);
                                handler.setRequestMethod(conn,HttpConnection.GET);
                                handler.sendRequestData(conn,"GET / HTTP/1.1");
                                if (conn.getResponseCode()==HttpConnection.HTTP_OK){
                                        System.out.println("发送请求成功");
                                        
                                        System.out.println("获得回应数据");
                                        String reponseData=(String)handler.getResponseData(conn);
                                        System.out.println(reponseData);
                                }
                                else{
                                        System.out.println("发送请求失败");
                                        System.out.println("错误信息:"+handler.getMessage());
                                }
                        }
                        else{
                                System.out.println("建立连接失败");
                        }
                }
                catch(Exception ex){
                        System.out.println("错误信息:"+handler.getMessage());
                        ex.printStackTrace();
                }
                
        }       
        protected void pauseApp() {

        }
        protected void destroyApp(boolean arg0) throws MIDletStateChangeException {            
        }

}

使用J2ME中的page进行编码转化使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化
  • 上一篇文章:

  • 下一篇文章:
  • 进入论坛讨论

    相关文章
    我的Thinking in Java学习笔记
    使用JBoss和Eclipse创建J2EE应用
    SpringFramework(9)
    NoTitle385
    向Web Service进军--Axis+Tomcat模拟一个银行存取款
    JAVA中的指针,引用及对象的clone
    JAVA中用动态代理类实现记忆功能(二)
    JAVA中用动态代理类实现记忆功能(一)
    DB Test
    基金系统的初步研究(1)
    SpringFramework(8)
    深入浅出Java clone技术(2)
    热门文章最新推荐

    版权与免责声明:
    ① 本网转载其他媒体稿件是为传播更多的信息,此类稿件不代表本网观点,版权归原作者所有,本网不承担此类稿件侵权行为的连带责任。
    ② 本站原创文章,转载时请注明出自文谷及作者姓名
    ③在本网BBS上发表言论者,文责自负。
    ④如您因版权等问题需要与本网联络,请在30日内联系 。
    使用J2ME中的page进行编码转化使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化
    使用J2ME中的page进行编码转化使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化2006-7-19 23:14:11使用J2ME中的page进行编码转化

    全站热点
    最新推荐
    关于文谷 | 联系文谷 | 免责声明 | 文谷论坛
    Tel: 0577-65690019      E-mail: ichenjian@gmail.com    MSN:ichenjian@hotmail.com    QQ:2911194
    Copyright © 2004-2008 wengu.com 文谷 All Rights Reserved
    浙ICP备05000327号