本文实例讲述了Android编程实现获取新浪天气预报数据的方法。分享给大家供大家参考,具体如下:
新浪天气预报地址:
http://php.weather.sina.com.cn/xml.php?city=武汉&password=DJOYnieT8234jlsK&day=0
其中,city后的城市可用java.net.URLEncoder.encode(“武汉”,” gb2312”);也可以直接写”武汉”,但不能用”wuhan”。Password固定
Day为0表示当天天气,1表示第二天的天气,2表示第三天的天气,以此类推,最大为4。
工具类:
1、定义成员
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/** 新浪天气网址 */
public final String SINA_URL = "http://php.weather.sina.com.cn/xml.php" ;
/** 新浪天气XML调用密码 */
public final String PASSWORD = "DJOYnieT8234jlsK" ;
/** 城市 */
public String city;
/** 白天天气 */
public String status1;
/** 夜晚天气 */
public String status2;
/** 白天天气 拼音 */
public String figure1;
/** 夜晚天气拼音 */
public String figure2;
/** 白天风向 */
public String direction1;
/** 夜晚风向 */
public String direction2;
/** 白天风级 */
public String power1;
/** 夜晚风级 */
public String power2;
/** 白天温度 */
public String temperature1;
/** 夜晚温度 */
public String temperature2;
/** 体感温度 */
public String tgd;
/** 紫外线指数 */
public String zwx_l;
/** 紫外线说明 */
public String zwx_s;
/** 体感度指数 */
public String ssd_l;
/** 体感度说明 */
public String ssd_s;
/** 空调指数 */
public String ktk_l;
/** 空调说明 */
public String ktk_s;
/** 洗车指数 */
public String xcz_l;
/** 洗车说明 */
public String xcz_s;
/** 穿衣指数 */
public String chy_l;
/** 穿衣说明 */
public String chy_shuoming;
/** 污染物扩散条件 */
public String pollution_l;
/** 污染物扩散条件说明 */
public String pollution_s;
/** 感冒指数 */
public String gm_l;
/** 感冒说明 */
public String gm_s;
/** 运动指数 */
public String yd_l;
/** 运动说明 */
public String yd_s;
|
2、获取天气数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/**
* 更新天气
*
* @param city
* 城市名
* @param day
* 0表示当天天气,1表示第二天的天气,2表示第三天的天气,以此类推,最大为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、访问网络
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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、关于jsoup可参考:
http://baike.baidu.com/view/4066913.htm
希望本文所述对大家Android程序设计有所帮助。