中国气象局天气API的中文乱码问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了中国气象局天气API的中文乱码问题相关的知识,希望对你有一定的参考价值。
String htmlStr = getHtmlFile("http://www.weather.com.cn/data/cityinfo/101010100.html"); try byte b[]=htmlStr.getBytes("ISO-8859-1"); htmlStr=new String(b); catch(Exception e) e.printStackTrace(); e.getMessage(); System.out.println(htmlStr); "weatherinfo":"city":"鍖椾含","cityid":"101010100","temp1":"29鈩?,"temp2":"19鈩?,"weather":"鏅?,"img1":"d0.gif","img2":"n0.gif","ptime":"11:00"
解决办法第一步:b[]=htmlStr.getBytes("ISO-8859-1"); htmlStr=new String(b);把ISO-8859-1换成utf-8
第二步:右键项目----》Properties(最后一个)---》Resource(第一个选项)-----》Other选择utf-8
给好评呦!!!
我都写出来了测试过了
java代码
public class abc
public static void main(String[] args) throws ClientProtocolException, IOException
getInfo();
public static void getInfo() throws ClientProtocolException, IOException
String htmlStr = getHtmlFile("http://www.weather.com.cn/data/cityinfo/101010100.html");
try
//ISO-8859-1 city":"北京","cityid":"101010100","temp1":"25�?,"temp2":"17�?,"weather":"小到中雨转阵�?,
//utf-8 "weatherinfo":"city":"鍖椾�?,"cityid":"101010100","temp1":"25鈩�,"temp2":"17鈩�,"weather":"灏忓埌涓洦杞�?���?
byte b[]=htmlStr.getBytes("utf-8"); htmlStr=new String(b);
catch(Exception e)
e.printStackTrace(); e.getMessage();
System.out.println(htmlStr);
public static String getHtmlFile(String url) throws ClientProtocolException, IOException
String result="";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse;
HttpGet httpGet = new HttpGet(url);
HttpEntity httpEntity;
InputStream is = null;
//ʹ�ÿͻ��˷�������
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = "";
while((line = reader.readLine()) !=null)
result = result + line;
return result;
参考技术A 编码问题,你要转换编码
城市代码cityCode 表格,用于获取中国省市天气的api
链接:https://pan.baidu.com/s/1hK1eTNByZiFO0xf4W8XZQQ
提取码:umf3
中国省市区及景点的cityCode都在这里。
使用方法:https://www.cnblogs.com/shangrao/p/12862475.html
以上是关于中国气象局天气API的中文乱码问题的主要内容,如果未能解决你的问题,请参考以下文章
中国天气网 天气预报API 国家气象局 根据城市名称抓取城市
城市代码cityCode 表格,用于获取中国省市天气的api
城市代码cityCode 表格,用于获取中国省市天气的api
天气预报API:中国气象频道腾讯天气--“新编码”接口的测试