《Android 網絡HTML查看器》一文中,運行代碼實踐一下
發現html源代碼中出現了亂碼,原因很明顯:charset="gb2312"
android默認的字符集是"utf-8"
public class StreamTools { /** * 把輸入流的內容轉化成字符串 * * @param is * @return */ public static String readInputStream(InputStream is) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } is.close(); baos.close(); byte[] result = baos.toByteArray(); return new String(result); } catch (Exception e) { e.printStackTrace(); return null; } } }
將上面標記的一行代碼修改為:
return new String(result, "gb2312");
運行后的效果如下
但是這樣修改后,不夠智能,如果遇到utf-8的編碼,又會出現亂碼。繼續修改代碼如下:
package com.wuyudong.htmlviewer.utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTools { /** * 把輸入流的內容轉化成字符串 * * @param is * @return */ public static String readInputStream(InputStream is) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } is.close(); baos.close(); byte[] result = baos.toByteArray(); String str = new String(result); // 試着解析result里面的字符串 if (str.contains("gb2312")) { return new String(result, "gb2312"); } else if(str.contains("utf-8")){ return str; } else { return null; } //return new String(result, "gb2312"); } catch (Exception e) { e.printStackTrace(); return null; } } }