Traditional Culture Encyclopedia - Weather inquiry - Chinese garbled code problem of weather API in China Meteorological Bureau

Chinese garbled code problem of weather API in China Meteorological Bureau

solution

Step 1: b [] = htmlstr.getbytes ("ISO-8859-1"); HtmlStr = new string (b); Replace ISO-8859- 1 with utf-8.

Step 2: Right-click the project-Properties (the last one)-Resources (the first option)-Other options utf-8.

Give a compliment! ! !

I wrote it all out for testing.

Java code

Public class abc {

Public static void main(String[] args) raises ClientProtocolException, IOException {

getInfo();

}

Public static void getInfo () throws ClientProtocolException, IOException{

string html str = gethtml file("/data/city info/ 10 10 10 100 . html ");

Try {

//ISO-8859- 1 city ":"Beijing ","CIYTID ":"101065438 ","temp 1 ":"25? ,“temp 2”:“ 17? "Weather": "Small to moderate rain? ,

//utf-8 {"weatherinfo":{"city ":"What? 、“city id”:“ 10 10 10 100”、“temp 1”:“25? ,“temp 2”:“ 17? Weather What are you doing? Yan Qi

byte b[]= html str . getbytes(" utf-8 "); HtmlStr = new string (b);

Catch (exception e) {

e . printstacktrace(); e . getmessage();

}

system . out . println(html str);

}

The public static string getHtmlFile (string url) raises ClientProtocolException, IOException{

String result = "";

default http client http client = new default http client();

HttpResponse httpResponse

http get http get = new http get(URL);

HttpEntity httpEntity

InputStream is = null.

//?

httpResponse = http client . execute(http get);

httpEntity = httpresponse . get entity();

is = http entity . get content();

buffered reader reader = new buffered reader(new InputStreamReader(is));

String line =

while((line = reader.readLine())! =null){

Result = Result+Line; }

Return the result;

}

}