《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; } } }
时间: 2024-10-01 23:11:23