| |
一、摘要
根据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 { }
}
|
|