This article describes the Java call to the China Weather Network API to obtain weather information. Share to everyone for your reference. The implementation method is as follows:
An example of Harbin Ice Country weather API call weather Information private String GetWeatherInfo2 () {StringBuilder info = new StringBuilder ();
try {defaulthttpclient httpclient = new Defaulthttpclient ();
HttpGet httget = new HttpGet ("http://m.weather.com.cn/data/101050101.html");
responsehandler<string> ResponseHandler = new Basicresponsehandler ();
String responsebody = Httpclient.execute (Httget, ResponseHandler);
System.out.println (responsebody);
Jsonparser JP = new Jsonparser ();
Jsonelement JSE = Jp.parse (responsebody);
Jsonobject JSO = Jse.getasjsonobject (). Get ("Weatherinfo"). Getasjsonobject ();
String updtime = Jso.get ("Fchh"). Getasstring (); if (updtime!= null) {////Temp//String j = Jso.get ("Temp1"). getasstring ();/Today//String m = Jso.get ("Temp2" ). Getasstring ()///Tomorrow////weather//String j_weather = Jso.get ("Weather1"). Getasstring ()//Today//String m_weather = Jso.get ("Weather2"). Getasstring ()//morrow///Wind wind//String J_wind = Jso.get ("Wind1"). getasstring()//Today//String M_wind = Jso.get ("Wind2"). Getasstring ()//Tomorrow//Info.append ("Today:"). Append (j). Append (""). Append (j_
Weather). Append (""). Append (J_wind). Append ("\ n");
Info.append ("Tomorrow:"). Append (M). Append (""). Append (M_weather). Append (""). Append (M_wind). Append ("\ 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_weather = Jso.get ("Weather1"). Getasstring (); /Today string m_weather = Jso.get ("Weather2"). Getasstring ()//Tomorrow//Wind wind String j_wind = Jso.get ("Wind1"). Getas string ();/today string m_wind = Jso.get ("Wind2"). Getasstring ()//Tomorrow Info.append ("Today:"). Append (j). Append (""). Appen
D (J_weather). Append (""). Append (J_wind). Append ("\ n");
Info.append ("Tomorrow:"). Append (M). Append (""). Append (M_weather). Append (""). Append (M_wind). Append ("\ n");
}else{ 18//Temperature String temp1 = Jso.get ("Temp1"). Getasstring ()//today string temp2 = Jso.get ("Temp2"). Getasstring ( //Today string Temp3 = Jso.get ("Temp3"). Getasstring ()//Today String j = Temp1.split ("~") [1] + "~" + temp2.split ("~"
) [0]; String m = Temp2.split ("~") [1] + "~" + temp3.split ("~") [0];//tomorrow//weather situation String Weather1 = Jso.get ("Weather1"). Get
Asstring ();
String weather2 = Jso.get ("Weather2"). Getasstring ();
String Weather3 = Jso.get ("Weather3"). Getasstring ();
String j_weather = "";
String j_weather_part1 = "";
String j_weather_part2 = "";
Determine if there is a turn if (Weather1.indexof ("Go") > 0) {//has J_weather_part1 = Weather1.split ("Turn") [1];
}else{j_weather_part1 = Weather1;
} if (Weather2.indexof ("ext") > 0) {//has J_weather_part2 = Weather2.split ("Turn") [0];
}else{j_weather_part2 = Weather2; } if (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_weather_part1 = "";
String m_weather_part2 = "";
Determine if there is a turn if (Weather2.indexof ("Go") > 0) {//has M_weather_part1 = Weather2.split ("Turn") [1];
}else{m_weather_part1 = Weather2;
} if (Weather3.indexof ("ext") > 0) {//has M_weather_part2 = Weather3.split ("Turn") [0];
}else{m_weather_part2 = Weather3; } if (M_weather_part1.equalsignorecase (M_weather_part2)) {M_weather = m_weather_part1;//today}else{m_w Eather = M_weather_part1 + "Turn" + m_weather_part2;//Tomorrow}//wind wind String j_wind = Jso.get ("Wind2"). getasstring ()//Today String M_wind = Jso.get ("Wind3"). Getasstring ()//Tomorrow Info.append ("Today:"). Append (j). Append (""). Append (j_we
ather). Append (""). Append (J_wind). Append ("\ n"); Info.append ("Tomorrow:"). Append (M). Append (""). Append (M_weather). Append (""). Append(M_wind). Append ("\ n");
The catch (Exception e) {} return info.tostring ();
}
I hope this article will help you with your Java programming.