public static String unzipHTML(String s){ int endPos = s.indexOf(" "); if(endPos<10) return s; try{ String header = s.substring(0, endPos); if(header.indexOf("Content-Encoding: gzip")!=-1){ String[] strs = s.split(" ", 2); Log.d("com.she.jyass.c", strs[0] + " strs[1] len=" + strs[1].length()); ByteArrayInputStream bis = new ByteArrayInputStream(strs[1].getBytes("Latin1")); GZIPInputStream g = new GZIPInputStream(bis); BufferedReader br = new BufferedReader(new InputStreamReader(g)); StringBuffer sb = new StringBuffer(); char[] buffer = new char[10240]; int len; while ( (len=br.read(buffer)) >0) { sb.append(buffer, 0, len); } s = strs[0] + " " + sb.toString(); } }catch(Exception e){ commonUtils.printError(e, "unzipHTML"); } return s; }
使用方法:
String s = new String(rtn.data, "Latin1"); s = commonUtils.unzipHTML(s);
注意,即使你的网页是utf-8的,也要用Latin1,否则出来的是乱码