Recent projects selected from the Chinese Weather network to take weather forecast data, API address is: http://www.weather.com.cn/data/cityinfo/101110101.html. 101110101 of which is the city's code, the specific needs of the city, the city code can be searched in Baidu.
At the beginning of the data to be taken to the Chinese and symbols are garbled, and finally through the following methods to solve. First define the Fetch method:
String url = "http://www.weather.com.cn/data/cityinfo/101110101.html";
String weatherinfo = weatherutil.getweatherinfo (URL);
Weatherutil for the acquisition of specific meteorological data of the tool class:
public static string Getweatherinfo (string url) {closeablehttpclient client;
Client = Httpclients.createdefault ();
HttpGet get = new HttpGet (URL);
HttpResponse response;
try {response = Client.execute (get);
httpentity entity = response.getentity ();
if (entity!= null) {InputStream instreams = entity.getcontent ();
String str = weatherutil.convertstreamtostring (instreams);
Get.abort ();
return str;
} catch (IOException e) {e.printstacktrace ();
return null;
private static String convertstreamtostring (InputStream is) {StringBuilder sb1 = new StringBuilder ();
byte[] bytes = new byte[4096];
int size; try {while (size = is.read (bytes) > 0) {String str = new String (bytes, 0, size, "UTF-8"
); Sb1.append (str);
} catch (IOException e) {e.printstacktrace ();
Finally {try {is.close ();
catch (IOException e) {e.printstacktrace ();
} return sb1.tostring (); }
This will solve the problem of returning garbled