java天氣預報查詢

 

public void getWeather() throws Exception{html

     

    //根據關鍵字搜索地址代碼app

    String url = "http://toy1.weather.com.cn/search";url

    HttpUtils util = HttpUtils.get(url);code

    util.addParameter("cityname", "上海");htm

    util.addParameter("callback", "");ci

    util.addParameter("_", System.currentTimeMillis()+"");get

    util.addHeader("Referer", "http://www.weather.com.cn/weather1d/101020100.shtml");string

     

    String ret = util.execute().getString();it

    if (ret == null || (ret=ret.trim()).equals("")) {io

        System.out.println("沒有查詢到地址信息");

        return ;

    }

     

    ret = ret.substring(1, ret.length()-1);

    Map[] list = HttpUtils.mapper.readValue(ret, Map[].class);

    if (list.length == 0) {

        System.out.println("沒有查詢到地址信息");

        return ;

    }

    System.out.println("查詢到的地址列表:"  + Arrays.toString(list));

     

    //獲取城市代碼

    String cityCode = list[0].get("ref").toString().substring(0, 9);

    url = "http://d1.weather.com.cn/sk_2d/" + cityCode + ".html?_=" + System.currentTimeMillis();

     

    //獲取天氣信息

    HttpUtils reqest = HttpUtils.get(url, util); //保持會話

    util.addHeader("Referer", "http://www.weather.com.cn/weather1d/101180101.shtml");

    ret = reqest.execute().getString();

    System.out.println(ret);

    util.shutdown();

}

相關文章
相關標籤/搜索