Android programming method to obtain Sina weather forecast data
- 2020-10-23 20:17:29
- OfStack
This paper illustrates the Android programming method to obtain sina weather forecast data. To share for your reference, the details are as follows:
Sina Weather Forecast Address:
http://php.weather.sina.com.cn/xml.php? Wuhan city = & password=DJOYnieT8234jlsK & day=0
Among them, the city available after city java. net. URLEncoder. encode (gb2312 "wuhan", ""); You can also write "Wuhan", but not "wuhan". Password fixed
Day is 0 for the weather of the day, 1 for the weather of the second day, 2 for the weather of the third day, and so on, the maximum is 4.
Tools:
1. Define members
/** Sina Weather website */
public final String SINA_URL = "http://php.weather.sina.com.cn/xml.php";
/** Sina's weather XML Call the password */
public final String PASSWORD = "DJOYnieT8234jlsK";
/** city */
public String city;
/** The weather during the day */
public String status1;
/** At night the weather */
public String status2;
/** The weather during the day pinyin */
public String figure1;
/** Night weather pinyin */
public String figure2;
/** The wind in the daytime */
public String direction1;
/** At night the wind */
public String direction2;
/** Takes place during the day */
public String power1;
/** The night wind scale */
public String power2;
/** Temperature during the day */
public String temperature1;
/** Night temperature */
public String temperature2;
/** Body feeling temperature */
public String tgd;
/** Uv index */
public String zwx_l;
/** Ultraviolet description */
public String zwx_s;
/** Somatosensory index */
public String ssd_l;
/** Somatosensory description */
public String ssd_s;
/** Air conditioning index */
public String ktk_l;
/** Air conditioning instructions */
public String ktk_s;
/** Washing index */
public String xcz_l;
/** Washing instructions */
public String xcz_s;
/** Dressing index */
public String chy_l;
/** Dress show */
public String chy_shuoming;
/** Pollutant diffusion condition */
public String pollution_l;
/** Description of pollutant diffusion conditions */
public String pollution_s;
/** A cold index */
public String gm_l;
/** A cold that */
public String gm_s;
/** Movement index */
public String yd_l;
/** Sports show */
public String yd_s;
2. Get weather data
/**
* Update the weather
*
* @param city
* City name
* @param day
* 0 Is the weather of the day, 1 According to the first 2 The weather of the day, 2 According to the first 3 The weather of the day, and so on, at most 4
*/
public void UpdateWeatherInfo(String city, String day) {
if (city.equals("")) {
isLoaded = false;
return;
}
String html = null;
try {
html = doGet(SINA_URL + "?city="
+ java.net.URLEncoder.encode(city, "gb2312") + "&password="
+ PASSWORD + "&day=" + day);
Document doc = Jsoup.parse(html);
if (doc.body().getElementsByTag("Profiles").size() == 0) {
isLoaded = false;
return;
}
if (doc.body().getElementsByTag("Profiles").get(0).getElementsByTag("Weather").size() == 0) {
isLoaded = false;
return;
}
Element element = doc.body().getElementsByTag("Profiles").get(0)
.getElementsByTag("Weather").get(0);
this.city = element.getElementsByTag("city").text();
status1 = element.getElementsByTag("status1").text();
status2 = element.getElementsByTag("status2").text();
figure1 = element.getElementsByTag("figure1").text();
figure2 = element.getElementsByTag("figure2").text();
direction1 = element.getElementsByTag("direction1").text();
direction2 = element.getElementsByTag("direction2").text();
power1 = element.getElementsByTag("power1").text();
power2 = element.getElementsByTag("power2").text();
temperature1 = element.getElementsByTag("temperature1").text();
temperature2 = element.getElementsByTag("temperature2").text();
tgd = element.getElementsByTag("tgd").text();
zwx_l = element.getElementsByTag("zwx_l").text();
zwx_s = element.getElementsByTag("zwx_s").text();
ssd_l = element.getElementsByTag("ssd_l").text();
ssd_s = element.getElementsByTag("ssd_s").text();
ktk_l = element.getElementsByTag("ktk_l").text();
ktk_s = element.getElementsByTag("ktk_s").text();
xcz_l = element.getElementsByTag("xcz_l").text();
xcz_s = element.getElementsByTag("xcz_s").text();
chy_l = element.getElementsByTag("chy_l").text();
chy_shuoming = element.getElementsByTag("chy_shuoming").text();
pollution_l = element.getElementsByTag("pollution_l").text();
pollution_s = element.getElementsByTag("pollution_s").text();
gm_l = element.getElementsByTag("gm_l").text();
gm_s = element.getElementsByTag("gm_s").text();
yd_l = element.getElementsByTag("yd_l").text();
yd_s = element.getElementsByTag("yd_s").text();
isLoaded = true;
} catch (UnsupportedEncodingException e) {
isLoaded = false;
}
}
3. Access the network
public static final String ENCODE = "utf-8";
public static String doGet(String url) {
try {
HttpGet httpGet = new HttpGet(url);
HttpClient hc = new DefaultHttpClient();
HttpResponse ht = hc.execute(httpGet);
if (ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity he = ht.getEntity();
InputStream is = he.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String response = "";
String readLine = null;
while ((readLine = br.readLine()) != null) {
response = response + readLine;
}
is.close();
br.close();
return response;
} else {
return "error";
}
} catch (Exception e) {
return "error";
}
}
4. Regarding jsoup, please refer to:
http://baike.baidu.com/view/4066913.htm
I hope this article has helped you with the Android programming.