The example of this article tells the way Java calls China Weather Network API to obtain weather forecast information. Share it for everyone for your reference. The specific implementation method is as follows:
// Take Bingcheng Harbin as an example to call the weather information through the Chinese weather API. ulthttpclient (); httpget httget = new httpget ("http: // m.weather.com.cn/data/101050101.html "); Responsehandler <string> Responsehandler = New Basicresponsehandler (); String Responsebody = httpclient.exec UTE (httget, responsehandler); System.out.println (responsebody); jsonparser JP = new jsonparser (); jsonelement jse = jp.parse (responsebody); jsonObject JSO = jse.getasjsonObject (). GetasjsonObject (); // raing updtime = jso.get ("fchh"). getasstring (); // if (updtime! = NULL) {// // Temperature // string j = jso.get ("temp1"). Getasstring (); // today // string m = jso.get ("" "" "" " temp2 "). Getasstring (); // Tomorrow // // weather conditions // string j_weather = jso.get (" weather1 "). Getasstring (); // today // string m_weether = jso.get (" weather2 "" ("weather2" "" ) .getasstring (); // Tomorrow // // Wind direction and wind // string j_wind = jso.get ("wind1"). Getasstring (); // Today // string m_wind = jso.get ("Wind2"). getasstring (); // tomorrow // info.append ("today:"). APPEND (J) .appnd ("") .appnd (j_weather) .appnd ("") .appnd (j_wind) .appnd ("/////////// n "); // Info.append (" "Tomorrow:"). APPEND (M) .appnd ("") .appnd (m_weather) .appnd ("") .appnd (m_wind) .aPend ("/n") ; //} string updtime = jso.get ("" fchh "). Getasstring (); if (updtime! = Null) {if (! Updtime.trim (). Equals (" 18 ")) {// temperature string j = jso.get ("temp1"). Getasstring (); // Today string m = jso.get ("temp2"). Getasstring (); // Tomorrow // weather situation string j_weether = jso.get ("weather1" ("weather1" " ) .getasstring (); // Today String m_weather = jso.get ("weather2"). Getasstring (); // Tomorrow // Wind direction wind force string j_wind = jso.get ("wind1"). Getasstring (); // Today String m_wind = jso.get ("Wind2"). Getasstring (); // tomorrow info.appnd ("today:"). APPEND (J) .appnd ("") .appnd (j_weather). ) .appnd (j_wind) .appnd ("/n"); info.append ("" Tomorrow: "). AppND (m) .appnd (" ") .appnd (m_weather) .appnd (" ") .append (m_wind ) .appnd ("/n");} Else {// 18 // Temperature string temp1 = jso.get ("temp1"). Getasstring (); // Today string test2 = jso.get ("Temp2"). getasstring (); // Today string temp3 = jso.get ("temp3"). Getasstring (); // Today string j = test1.split ("~") ~ ") [0]; string m = temp2.split (" ~ ") [1] +" ~ " + Temp3.split (" ~ ") [0]; // Tomorrow // Weather condition string weather1 = jso. get ("weather1"). Getasstring (); string weather2 = jso.get ("weather2"). Getasstring (); string weather3 = jso.get ("weather3"). ather = ""; string j_weather_part1 = ""; string j_weather_part2 = "" "; // Determine whether there is a transfer if (weather1.indexof (" turn ")> 0) {// J_weather_part1 = weather1.split (" turn ") [1];} elsee {j_weather_part1 = weather1;} if (weather2.indexof ("turn")> 0) {// J_weather_part2 = weather2.split ("turn") [0];} else {j_weather_part2 = weather2;} I f (j_weather_part1.equalSignorecase (j_weather_part2)) {j_weather = j_weather_part1; // Today} else {j_weather = j_weather_part1 + "turn" + j_weather_part2; // Today} String m_weather = ""; string m_w Eather_part1 = ""; string m_weather_part2 = ""; // judge Is there any turn (weather2.indexof ("turn")> 0) {// There is m_weather_part1 = weather2.split ("turn") [1];} else {m_weather_part1 = weather2;} if (weather3.indexof ("turn (" turn to ") > 0){ //有m_weather_part2 = weather3.split("转")[0]; }else{ m_weather_part2 = weather3; } if(m_weather_part1.equalsIgnoreCase(m_weather_part2)){ m_weather = m_weather_part1;//今天}else {m_weather = m_weather_part1 + "turn" + m_weather_part2; // Tomorrow} // wind direction strong j_wind = jso.get ("wind2"). Getasstring (); // Today string m_wind = 3 ").. getasstring () ;/ tomorrow info.append ("today:"). APPEND (J) .appnd ("") .appnd (j_weather) .appnd ("") .appnd (j_wind) .appnd ("/n" ("/n" );; ("" Tomorrow: "). APPEND (M) .appnd (" ") .appnd (m_Weather) .appnd (" ") .aPend (m_wind) .appnd ("/n ");}}}}}}}}}}}}}}}}}}}}}}}}}} catch (Exception E) {} Return info.tostring ();}
It is hoped that this article is helpful to everyone's Java program design.