歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android簡單的獲取不同城市實時天氣預報【附源碼】

Android簡單的獲取不同城市實時天氣預報【附源碼】

日期:2017/3/1 9:55:20   编辑:Linux編程

該程序在上一個程序的基礎上 改進 添加了獲取天氣狀態

前一篇文章主要實現 獲取城市編號 不熟悉可以參加 http://www.linuxidc.com/Linux/2013-07/86986.htm

該程序調用中央氣象局API 有多種數據:

http://www.weather.com.cn/data/sk/101010100.html 簡單的實時天氣json 數據
http://m.weather.com.cn/data/101270101.html 包含詳細的天氣預報信息

本文源碼下載: http://www.linuxidc.com/Linux/2013-07/86986.htm

後者 如下例子:

{"weatherinfo":{
"city":"武漢",
"city_en":"wuhan",
"date_y":"2012年4月20日",
"date":"",
"week":"星期五","fchh":"11",
"cityid":"101200101",
"temp1":"21℃~12℃",
"temp2":"27℃~14℃",
"temp3":"28℃~17℃",
"temp4":"30℃~19℃",
"temp5":"32℃~22℃",
"temp6":"29℃~13℃",
"tempF1":"69.8℉~53.6℉",
"tempF2":"80.6℉~57.2℉",
"tempF3":"82.4℉~62.6℉",
"tempF4":"86℉~66.2℉",
"tempF5":"89.6℉~71.6℉",
"tempF6":"84.2℉~55.4℉",
"weather1":"陣雨轉多雲",
"weather2":"多雲",
"weather3":"多雲",
"weather4":"陰轉陣雨",
"weather5":"小雨轉陰",
"weather6":"陰",
"img1":"3",
"img2":"1",
"img3":"1",
"img4":"99",
"img5":"1",
"img6":"99",
"img7":"2",
"img8":"3",
"img9":"7",
"img10":"2",
"img11":"2",
"img12":"99",
"img_single":"3",
"img_title1":"陣雨",
"img_title2":"多雲",
"img_title3":"多雲",
"img_title4":"多雲",
"img_title5":"多雲",
"img_title6":"多雲",
"img_title7":"陰",
"img_title8":"陣雨",
"img_title9":"小雨",
"img_title10":"陰",
"img_title11":"陰",
"img_title12":"陰",
"img_title_single":"陣雨",
"wind1":"微風",
"wind2":"微風",
"wind3":"微風",
"wind4":"微風",
"wind5":"微風",
"wind6":"微風",
"fx1":"微風",
"fx2":"微風",
"fl1":"小於3級",
"fl2":"小於3級",
"fl3":"小於3級",
"fl4":"小於3級",
"fl5":"小於3級",
"fl6":"小於3級",
"index":"舒適",//穿衣指數
"index_d":"建議著薄型套裝或牛仔衫褲等春秋過渡裝。年老體弱者宜著套裝、夾克衫等。",
"index48":"舒適",
"index48_d":"建議著薄型套裝等春秋過渡裝。年老體弱者宜著套裝。但晝夜溫差較大,注意適當增減服。",
"index_uv":"弱",//紫外線指數
"index48_uv":"弱",//紫外線指數
"index_xc":"不宜", //洗車指數
"index_tr":"適宜", //旅游指數
"index_co":"舒適", //舒適指數
"st1":"17",
"st2":"10",
"st3":"28",
"st4":"15",
"st5":"29",
"st6":"17",
"index_cl":"較不宜", //晨練指數
"index_ls":"不太適宜", //晾曬指數
"index_ag":"極易發" //息斯敏過敏氣象指數
}
}

img1~ img12數字表示 見下表:

編號 名稱
0 晴
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 強沙塵暴
99 不變

前者例子:

{"weatherinfo"{"city":"北京",
"cityid":"101010100",//城市編號
"temp":"14",//實時氣溫
"WD":"東風",//風向
"WS":"級", //單位
"SD":"71%",//濕度
"WSE":"2", //風級
"time":"13:00",
"isRadar":"1",
"Radar":"JC_RADAR_AZ9010_JB"
}
}

只是數據不同 實現完全相同 簡單起見 使用後者進行解析

1配置文件 在前篇博客的基礎上加入互聯網權限

<uses-permission Android:name="android.permission.INTERNET"/>

2 WeatherInfo.java WeatherModel.java

package com.cityselection;

public class WeatherModel {

private WeatherInfo weatherinfo =new WeatherInfo();

public WeatherInfo getWeatherinfo() {
return weatherinfo;
}

public void setWeatherinfo(WeatherInfo weatherinfo) {
this.weatherinfo = weatherinfo;
}

}

package com.cityselection;

public class WeatherInfo {
private String city;//城市名稱
private String temp;//實時氣溫
private String WD;//風向
private String WS;//風級
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getWD() {
return WD;
}
public void setWD(String wD) {
WD = wD;
}
public String getWS() {
return WS;
}
public void setWS(String wS) {
WS = wS;
}

}

3.布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:text="省份/直轄市"
android:textSize="20dp"
android:text
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/provinces"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="市/縣"
android:textSize="20dp"
android:text
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/savecity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保持配置"/>
<TextView
android:id="@+id/city_name"
android:textSize="40dp"
android:text
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/temp"
android:textSize="20dp"
android:text
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/wind"
android:textSize="20dp"
android:text
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Copyright © Linux教程網 All Rights Reserved