HttpClient使用HttpGet获取Json数据出现部分数据乱码的情况

问题描述:
在SSM框架,Controller内部使用HttpClient获取Json数据,在某些中文后出现乱码 情况(只是某写中文),在该项目中使用普通Java类没有出现乱码情况。
最终解决方案:
度娘搜了很久才解决。
源代码:

	HttpClient httpclient = new DefaultHttpClient();
	 HttpGet httpGet = new HttpGet("http://data.api.gkcx.eol.cn/soudaxue/querySchoolgufenSpecial.html?messtype=jsonp&page=1&size=50&fsxxxS=%E6%B9%96%E5%8C%97&examineeType=%E7%90%86%E7%A7%91&mark=500&recommended=%E4%BF%9D%E5%BA%95&schoolprovince=&recomluqupici=&year=2015&callback=jQuery18304095057360827923_1463726501794&_=1463726502201");  
     httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"); 
     
     HttpResponse response = httpclient.execute(httpGet);  
      HttpEntity entity = response.getEntity();  
      StringBuilder result = new StringBuilder();  
      if (entity != null) {  
          InputStream instream = entity.getContent();  
          BufferedReader br = new BufferedReader(new InputStreamReader(instream));  
          String temp = "";  
          while ((temp = br.readLine()) != null) {  
              String str = new String(temp.getBytes(), "utf-8");  
              result.append(str);//.append("\r\n");  
          }  
      } 
      System.out.println(result2);  

修改后代码:

public void test() throws Exception{
	HttpClient httpclient = new DefaultHttpClient();
	 HttpGet httpGet = new HttpGet("http://data.api.gkcx.eol.cn/soudaxue/querySchoolgufenSpecial.html?messtype=jsonp&page=1&size=50&fsxxxS=%E6%B9%96%E5%8C%97&examineeType=%E7%90%86%E7%A7%91&mark=500&recommended=%E4%BF%9D%E5%BA%95&schoolprovince=&recomluqupici=&year=2015&callback=jQuery18304095057360827923_1463726501794&_=1463726502201");  
     httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"); 
     
     HttpResponse response = httpclient.execute(httpGet);  
      HttpEntity entity = response.getEntity();  
      String charset = "UTF-8";
      String result2 = "";
      if (entity != null) {  
    	  charset = getContentCharSet(entity);  
          // 使用EntityUtils的toString方法,传递编码,默认编码是ISO-8859-1   
       result2 = EntityUtils.toString(entity, charset);
      } 
        System.out.println(result2);      
}

public static String getContentCharSet(final HttpEntity entity)   
        throws Exception {   
        if (entity == null) {   
            throw new IllegalArgumentException("HTTP entity may not be null");   
        }   
        String charset = null;   
        if (entity.getContentType() != null) {    
            HeaderElement values[] = entity.getContentType().getElements();   
            if (values.length > 0) {   
                NameValuePair param = values[0].getParameterByName("charset" );   
                if (param != null) {   
                    charset = param.getValue();   
                }   
            }   
        }   
        if(StringUtils.isEmpty(charset)){  
            charset = "UTF-8";  
        }  
        return charset;   
    }  

由于jar包太多,不在展示了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值