<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 33,comments - 21,trackbacks - 0

    作者:cleverpig(作者的Blog:http://blog.matrix.org.cn/page/cleverpig)
    原文:http://www.matrix.org.cn/resource/article/44/44154_j2me.html
    關鍵字:j2me,頁面,編碼,亂碼
    一、摘要
    根據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 {
    ????????????????

    ????????}

    }


    更多問題,請到Matrix J2me版討論
    posted on 2007-03-19 14:27 英明 閱讀(257) 評論(0)  編輯  收藏 所屬分類: J2ME
    主站蜘蛛池模板: 亚洲精品中文字幕乱码三区 | 亚洲Av永久无码精品黑人 | aaa毛片免费观看| 亚洲av无码一区二区三区天堂古代 | 免费毛片在线播放| 96免费精品视频在线观看| 日日躁狠狠躁狠狠爱免费视频| 亚洲videosbestsex日本| 亚洲国产精品成人久久| 亚洲A丁香五香天堂网 | 在线观看亚洲精品专区| 亚洲图片校园春色| 久久久久亚洲AV成人无码网站| 亚洲成a人片在线观看久| 日本a级片免费看| 永久免费毛片在线播放| 99热在线免费播放| 久草视频在线免费看| 不卡视频免费在线观看| 日本一区二区在线免费观看| 亚洲欧美国产欧美色欲| 天堂亚洲国产中文在线| 亚洲冬月枫中文字幕在线看| 亚洲天堂一区二区三区| 亚洲欧洲精品在线| 亚洲精品亚洲人成在线观看麻豆| 久久久久无码精品亚洲日韩| 亚洲综合熟女久久久30p| 国产亚洲日韩一区二区三区| 丁香五月亚洲综合深深爱| 免费va在线观看| 亚洲国产精品专区在线观看| 免费一级毛片在级播放| 国产精品99久久免费| 国产精品另类激情久久久免费| 日本视频免费在线| 国产福利免费在线观看| 亚洲日本中文字幕天堂网| ZZIJZZIJ亚洲日本少妇JIZJIZ| 在线观看国产区亚洲一区成人| 91麻豆国产自产在线观看亚洲|