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

2022-05-06 19:27:58 浏览数 (2)

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

代码语言:javascript复制
	HttpClient httpclient = new DefaultHttpClient();
	 HttpGet httpGet = new HttpGet("http://data.api.gkcx.eol.cn/soudaxue/querySchoolgufenSpecial.html?messtype=jsonp&page=1&size=50&fsxxxS=湖北&examineeType=理科&mark=500&recommended=保底&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("rn");  
          }  
      } 
      System.out.println(result2);  

修改后代码:

代码语言:javascript复制
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=湖北&examineeType=理科&mark=500&recommended=保底&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 人点赞