Android 从网络中获取数据时 产生部分数据乱码的解决

转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/23562939

问题描述:从网络中抓取html页面进行解析,解析后发现产生部分中文乱码。

由来:制作csdn客户端时产生 , http://blog.csdn.net/lmj623565791/article/details/23532797  (java环境下,使用控制台打印出是没有乱码)


于是立马检查从服务器读取的代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">  /** 
  2.      * 返回该链接地址的html数据 
  3.      *  
  4.      * @param urlStr 
  5.      * @return 
  6.      * @throws CommonException 
  7.      */  
  8.     public static String doGet(String urlStr) throws CommonException  
  9.     {  
  10.         StringBuffer sb = new StringBuffer();  
  11.         try  
  12.         {  
  13.             URL url = new URL(urlStr);  
  14.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  15.             conn.setRequestMethod("GET");  
  16.             conn.setConnectTimeout(5000);  
  17.             conn.setDoInput(true);  
  18.             conn.setDoOutput(true);  
  19.   
  20.             if (conn.getResponseCode() == 200)  
  21.             {  
  22.                 InputStream is = conn.getInputStream();  
  23.                 int len = 0;  
  24.                 byte[] buf = new byte[1024];  
  25.   
  26.                 while ((len = is.read(buf)) != -1)  
  27.                 {  
  28.                     sb.append(new String(buf, 0, len, "UTF-8"));  
  29.                 }  
  30.   
  31.                 is.close();  
  32.             } else  
  33.             {  
  34.                 throw new CommonException("访问网络失败!");  
  35.             }  
  36.   
  37.         } catch (Exception e)  
  38.         {  
  39.             throw new CommonException("访问网络失败!");  
  40.         }  
  41.         return sb.toString();  
  42.     }  
  43. </span>  
 
发现可能是由于我采用字节流从网络读取数据,且每次读取1024个字节,读取完成后能后强制转化为字符串,又因为使用编码为UTF-8,UTF-8是一种变长码(英文1个字节,中文两个字节),所以1024可能会造成刚好截取了某个汉字的一半(前一个字节),然后转化为字符串时造成乱码。唯一不理解的在java环境下,使用控制台打印出是没有乱码的。如果你有不同的理解欢迎留言探讨。

于是把读取数据的代码从字节流改成字符流,修改后的代码为:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">  /** 
  2.      * 返回该链接地址的html数据 
  3.      *  
  4.      * @param urlStr 
  5.      * @return 
  6.      * @throws CommonException 
  7.      */  
  8.     public static String doGet(String urlStr) throws CommonException  
  9.     {  
  10.         StringBuffer sb = new StringBuffer();  
  11.         try  
  12.         {  
  13.             URL url = new URL(urlStr);  
  14.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  15.             conn.setRequestMethod("GET");  
  16.             conn.setConnectTimeout(5000);  
  17.             conn.setDoInput(true);  
  18.             conn.setDoOutput(true);  
  19.   
  20.             if (conn.getResponseCode() == 200)  
  21.             {  
  22.                 InputStream is = conn.getInputStream();  
  23.                 InputStreamReader isr = new InputStreamReader(is,"UTF-8");  
  24.                 int len = 0;  
  25.                 char[] buf = new char[1024];  
  26.   
  27.                 while ((len = isr.read(buf)) != -1)  
  28.                 {  
  29.                     sb.append(new String(buf, 0, len));  
  30.                 }  
  31.   
  32.                 is.close();  
  33.                 isr.close();  
  34.             } else  
  35.             {  
  36.                 throw new CommonException("访问网络失败!");  
  37.             }  
  38.   
  39.         } catch (Exception e)  
  40.         {  
  41.             throw new CommonException("访问网络失败!");  
  42.         }  
  43.         return sb.toString();  
  44.     }  
  45. </span>  
问题解决。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值