Solve the problem of garbled data in weather network weather in China

Source: Internet
Author: User
Tags int size

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.