Android 字元亂碼問題的處理,android亂碼
《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; } }}