The problem of Chinese garbled characters in the weather API of China Meteorological Administration
Solution
Step one: b[]=htmlStr.getBytes("ISO-8859-1"); htmlStr=new String(b); put ISO-8859- 1Replace with utf-8
Step 2: Right-click the project----》Properties (the last one)---》Resource (the first option)-----》Other select utf- 8
Give me a good review! ! !
I have written it and tested it
java code
public class abc {
public static void main(String[] args) throws ClientProtocolException, IOException {
getInfo();
}
public static void getInfo() throws ClientProtocolException, IOException{
String htmlStr = getHtmlFile("/data/cityinfo/101010100.html");
try{
//ISO-8859-1 city":"Beijing","cityid ":"101010100","temp1":"25?,"temp2":"17?,"weather":"Light to moderate rain?,
//utf-8 {"weatherinfo ":{"city":"鍖椾?,"cityid":"101010100","temp1":"25鈩?,"temp2":"17鈩?,"weather":"灏忓韌涓?榦杞
byte b[]=htmlStr.getBytes("utf-8"); htmlStr=new String(b);
} catch(Exception e) {
< p> 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();
p>BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = "";
while((line = reader.readLine()) ! =null){
result = result + line;}
return result;
}
}