适用于入门学习的综合性Android项目---获取全国各地实时天气预报
程序员文章站
2024-03-19 11:29:40
...
获取全国各地实时天气预报
开发工具
Eclipse集成Android开发环境(ADT)+Android实体机或者海马玩Android模拟器(推荐);
适用对象
基本了解java语法和Android的基本的activity原理,json数据解析等;
项目总体构造
效果运行效果
功能实现
1.通过调用基本天气接口,获取天气的数据
// 判断网络连接情况的方法
private void getWeatherDatafromNet(String cityCode) {
final String address = "http://wthrcdn.etouch.cn/WeatherApi?citykey=" + cityCode;
// Log.d("Address:",address);
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection urlConnection = null;
TodayWeather todayWeather = null;
try {
URL url = new URL(address);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(8000);
urlConnection.setReadTimeout(8000);
InputStream in = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String str;
while ((str = reader.readLine()) != null) {
sb.append(str);
// Log.d("kkkkkkkkkkkk",str);
}
String response = sb.toString();
// Log.d("kkkkkkkkkkkk",response);
todayWeather = parseXML(response);
if (todayWeather != null) {
// Log.d("kkkkkkkkkkkk", todayWeather.toString());
Message msg = new Message();
msg.what = UPDATE_TODAY_WEATHER;
msg.obj = todayWeather;
mHandler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
2.解析查询到的xml数据
// 解析xml方法
private TodayWeather parseXML(String xmlData) {
TodayWeather todayweather = null;
int fengliCount = 0;
int fengxiangCount = 0;
int dateCount = 0;
int highCount = 0;
int lowCount = 0;
int typeCount = 0;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xmlData));
int eventType = xmlPullParser.getEventType();
// Log.d("kkkkkkkkkkkkkkkk", "start parse xml");
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
// 文档开始位置
case XmlPullParser.START_DOCUMENT:
// Log.d("kkkkkkkkkkkkkkkk", "start doc");
break;
// 标签元素开始位置
case XmlPullParser.START_TAG:
if (xmlPullParser.getName().equals("resp")) {
todayweather = new TodayWeather();
}
if (todayweather != null) {
if (xmlPullParser.getName().equals("city")) {
eventType = xmlPullParser.next();
// Log.d("city", xmlPullParser.getText());
todayweather.setCity(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("updatetime")) {
eventType = xmlPullParser.next();
// Log.d("updatetime", xmlPullParser.getText());
todayweather.setUpdatetime(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("wendu")) {
eventType = xmlPullParser.next();
// Log.d("wendu", xmlPullParser.getText());
todayweather.setWendu(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("fengli") && fengliCount == 0) {
eventType = xmlPullParser.next();
// Log.d("fengli", xmlPullParser.getText());
todayweather.setFengli(xmlPullParser.getText());
fengliCount++;
} else if (xmlPullParser.getName().equals("shidu")) {
eventType = xmlPullParser.next();
// Log.d("shidu", xmlPullParser.getText());
todayweather.setShidu(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("fengxiang") && fengxiangCount == 0) {
eventType = xmlPullParser.next();
// Log.d("fengxiang", xmlPullParser.getText());
fengxiangCount++;
todayweather.setFengxiang(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("pm25")) {
eventType = xmlPullParser.next();
// Log.d("pm25", xmlPullParser.getText());
todayweather.setPm25(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("quality")) {
eventType = xmlPullParser.next();
// Log.d("quelity", xmlPullParser.getText());
todayweather.setQuality(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("date") && dateCount == 0) {
eventType = xmlPullParser.next();
// Log.d("date", xmlPullParser.getText());
dateCount++;
todayweather.setDate(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("high") && highCount == 0) {
eventType = xmlPullParser.next();
// Log.d("high", xmlPullParser.getText());
highCount++;
todayweather.setHigh(xmlPullParser.getText().substring(2).trim());
} else if (xmlPullParser.getName().equals("low") && lowCount == 0) {
eventType = xmlPullParser.next();
// Log.d("low", xmlPullParser.getText());
lowCount++;
todayweather.setLow(xmlPullParser.getText().substring(2).trim());
} else if (xmlPullParser.getName().equals("type") && typeCount == 0) {
eventType = xmlPullParser.next();
// Log.d("type", xmlPullParser.getText());
typeCount++;
todayweather.setType(xmlPullParser.getText());
}
}
break;
// 判断当前事件是否为标签元素结束事件
case XmlPullParser.END_TAG:
break;
}
eventType = xmlPullParser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return todayweather;
}
3.解析后的数据实时的更新到Android的UI界面,并做Toast输出提醒
// 利用TodayWeather对象更新UI中的控件
void updateTodayWeather(TodayWeather todayWeather) {
city.setText(todayWeather.getCity());
time.setText(todayWeather.getUpdatetime() + "发布");
wendu.setText("当前温度:" + todayWeather.getWendu() + "°C");
shidu.setText("当前湿度:" + todayWeather.getShidu());
pm.setText(todayWeather.getPm25());
pmlevel.setText("空气质量:" + todayWeather.getQuality());
week.setText(todayWeather.getDate());
range.setText(todayWeather.getLow() + "~" + todayWeather.getHigh());
tianqi.setText(todayWeather.getType());
feng.setText(todayWeather.getFengxiang() + todayWeather.getFengli());
if (todayWeather.getPm25() != null) {
int pm25 = Integer.parseInt(todayWeather.getPm25());
if (pm25 <= 50) {
imgpm.setImageResource(R.drawable.pm_0_50);
} else if (pm25 >= 51 && pm25 <= 100) {
imgpm.setImageResource(R.drawable.pm_51_100);
} else if (pm25 >= 101 && pm25 <= 150) {
imgpm.setImageResource(R.drawable.pm_101_150);
} else if (pm25 >= 151 && pm25 <= 200) {
imgpm.setImageResource(R.drawable.pm_151_200);
} else if (pm25 >= 201 && pm25 <= 300) {
imgpm.setImageResource(R.drawable.pm_201_300);
} else if (pm25 > 300) {
imgpm.setImageResource(R.drawable.pm_300);
}
}
// if (todayWeather.getType() != null) {
// switch (todayWeather.getType()) {
// case "晴":
// imgtianqi.setImageResource(R.drawable.qing);
// break;
// case "阴":
// imgtianqi.setImageResource(R.drawable.yin);
// break;
// case "雾":
// imgtianqi.setImageResource(R.drawable.wu);
// break;
// case "多云":
// imgtianqi.setImageResource(R.drawable.duoyun);
// break;
// case "小雨":
// imgtianqi.setImageResource(R.drawable.xiaoyu);
// break;
// case "中雨":
// imgtianqi.setImageResource(R.drawable.zhongyu);
// break;
// case "大雨":
// imgtianqi.setImageResource(R.drawable.dayu);
// break;
// case "阵雨":
// imgtianqi.setImageResource(R.drawable.zhenyu);
// break;
// case "雷阵雨":
// imgtianqi.setImageResource(R.drawable.leizhenyu);
// break;
// case "雷阵雨冰雹":
// imgtianqi.setImageResource(R.drawable.leizhenyubingbao);
// break;
// case "暴雨":
// imgtianqi.setImageResource(R.drawable.baoyu);
// break;
// case "大暴雨":
// imgtianqi.setImageResource(R.drawable.dabaoyu);
// break;
// case "特大暴雨":
// imgtianqi.setImageResource(R.drawable.tedabaoyu);
// break;
// case "阵雪":
// imgtianqi.setImageResource(R.drawable.zhenxue);
// break;
// case "暴雪":
// imgtianqi.setImageResource(R.drawable.baoxue);
// break;
// case "大雪":
// imgtianqi.setImageResource(R.drawable.daxue);
// break;
// case "小雪":
// imgtianqi.setImageResource(R.drawable.xiaoxue);
// break;
// case "雨夹雪":
// imgtianqi.setImageResource(R.drawable.yujiaxue);
// break;
// case "中雪":
// imgtianqi.setImageResource(R.drawable.zhongxue);
// break;
// case "沙尘暴":
// imgtianqi.setImageResource(R.drawable.shachenbao);
// break;
// default:
// break;
// }
// }
Toast.makeText(MainActivity.this, "更新成功!", Toast.LENGTH_SHORT).show();
}
涉及到的技术
1.Android中各种Activity的调用和相互数据传递
2.Java中对数据对象的简单的分装处理;
3.利用java对xml格式的数据进行解析处理;
4.通过调用数据接口,获取数据,存储到Android的sqlite数据库中;
5.基本的AndroidUI特效的实现;
项目中各个类源码展示
City.java
package com.example.bean;
/**
*
* @Description:封装天气类对象;
* @author: Taylor
* @Time: 2018年6月21日
*
*/
public class City {
private String province;
private String city;
private String number;
private String firstPY;
private String allPY;
private String allFristPY;
public City(String province, String city, String number,
String firstPY, String allPY, String allFristPY) {
this.province = province;
this.city = city;
this.number = number;
this.firstPY = firstPY;
this.allPY = allPY;
this.allFristPY = allFristPY;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getFirstPY() {
return firstPY;
}
public void setFirstPY(String firstPY) {
this.firstPY = firstPY;
}
public String getAllPY() {
return allPY;
}
public void setAllPY(String allPY) {
this.allPY = allPY;
}
public String getAllFristPY() {
return allFristPY;
}
public void setAllFristPY(String allFristPY) {
this.allFristPY = allFristPY;
}
}
TodayWeather.java
package com.example.bean;
/**
*
* @Description:对当前天气的对象的封装
* @author: Taylor
* @Time: 2018年6月21日
*
*/
public class TodayWeather {
private String city;
private String updatetime;
private String wendu;
private String shidu;
private String pm25;
private String quality;
private String fengxiang;
private String fengli;
private String date;
private String high;
private String low;
private String type;
// get,set方法
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getUpdatetime() {
return updatetime;
}
public void setUpdatetime(String updatetime) {
this.updatetime = updatetime;
}
public String getWendu() {
return wendu;
}
public void setWendu(String wendu) {
this.wendu = wendu;
}
public String getShidu() {
return shidu;
}
public void setShidu(String shidu) {
this.shidu = shidu;
}
public String getPm25() {
return pm25;
}
public void setPm25(String pm25) {
this.pm25 = pm25;
}
public String getQuality() {
return quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
public String getFengxiang() {
return fengxiang;
}
public void setFengxiang(String fengxiang) {
this.fengxiang = fengxiang;
}
public String getFengli() {
return fengli;
}
public void setFengli(String fengli) {
this.fengli = fengli;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "TodayWeather{" +
"city='"+ city +'\''+
",updatetime='"+ updatetime +'\''+
",wendu='"+ wendu +'\''+
",shidu='"+ shidu +'\''+
",pm25='"+ pm25 +'\''+
",quality='"+ quality +'\''+
",fengxiang='"+ fengxiang +'\''+
",fengli='"+ fengli +'\''+
",date='"+ date +'\''+
",high='"+ high +'\''+
",low='"+ low +'\''+
",type='"+ type +'\''+
'}';
}
}
CityDB.java
package com.example.CityDB;
import java.util.ArrayList;
import java.util.List;
import com.example.bean.City;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class CityDB {
public static final String CITY_DB_NAME = "city.db";
private static final String CITY_TABLE_NAME = "city";
private SQLiteDatabase db;
public CityDB(Context context, String path) {
db = context.openOrCreateDatabase(path, Context.MODE_PRIVATE, null);
}
public List<City> getAllCity() {
List<City> list = new ArrayList<City>();
Cursor c = db.rawQuery("SELECT * from " + CITY_TABLE_NAME, null);
while (c.moveToNext()) {
String province = c.getString(c.getColumnIndex("province"));
String city = c.getString(c.getColumnIndex("city"));
String number = c.getString(c.getColumnIndex("number"));
String allPY = c.getString(c.getColumnIndex("allpy"));
String allFirstPY = c.getString(c.getColumnIndex("allfirstpy"));
String firstPY = c.getString(c.getColumnIndex("firstpy"));
City item = new City(province, city, number, firstPY, allPY,allFirstPY);
list.add(item);
}
return list;
}
}
CityManager.java
package com.example.myweatherdemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import com.example.bean.City;
import com.example.myweatherdemo1.R;
/**
*
* @Description:将天气信息填充到适配器中,做输出UI样式显示处理;
* @author: Taylor
* @Time: 2018年6月21日
*
*/
public class CityManager extends Activity implements OnClickListener {
private ImageView backBtn;
private ListView citylist_lv;
private List<City> mCitylist;
private MyApplication mApplication;
private ArrayList<String> mArrayList;
private String updateCityCode="-1";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题
setContentView(R.layout.city_manager);
backBtn=(ImageView) findViewById(R.id.back_city);
backBtn.setOnClickListener(this);
mApplication=(MyApplication)getApplication();
mCitylist=mApplication.getCityList();
mArrayList=new ArrayList<String>();//不new会指向空
for (int i = 0; i < mCitylist.size(); i++) {
String cityname=mCitylist.get(i).getCity();
mArrayList.add(cityname);
}
citylist_lv=(ListView) findViewById(R.id.selectcity_lv);
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(CityManager.this,android.R.layout.simple_list_item_1,mArrayList);
citylist_lv.setAdapter(adapter);
//添加listview的点击事件动作
AdapterView.OnItemClickListener itemClickListener=new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int aaa=Integer.parseInt(mCitylist.get(position).getNumber());
updateCityCode=Integer.toString(aaa);
}
};
citylist_lv.setOnItemClickListener(itemClickListener);
}
public void onClick(View v) {
switch(v.getId())
{
case R.id.back_city:
//传输citycode数据
Intent i = new Intent(this,MainActivity.class);
i.putExtra("cityCode", updateCityCode);
setResult(RESULT_OK, i);
finish();
break;
default:
break;
}
}
}
MainActivity.java
package com.example.myweatherdemo;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.bean.TodayWeather;
import com.example.myweatherdemo1.R;
/**
*
* @Description:
* 主Activity,对项目的主要功能进行控制
* @author: Taylor
* @Time: 2018年6月21日
*
*/
public class MainActivity extends Activity implements OnClickListener {
private static final int UPDATE_TODAY_WEATHER = 1;
private ImageView updateBtn, cityBtn;
private TextView city, time, wendu, shidu, pm, pmlevel, week, range, tianqi, feng;
private ImageView imgtianqi, imgpm;
// 刷新天气数据
private Handler mHandler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case UPDATE_TODAY_WEATHER:
updateTodayWeather((TodayWeather) msg.obj);
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题
setContentView(R.layout.activity_main);
// 按钮点击事件
updateBtn = (ImageView) findViewById(R.id.update_image);
updateBtn.setOnClickListener(this);
cityBtn = (ImageView) findViewById(R.id.city_manager);
cityBtn.setOnClickListener(this);
// 初始化textview控件
city = (TextView) findViewById(R.id.city_name);
time = (TextView) findViewById(R.id.time);
wendu = (TextView) findViewById(R.id.nowwendu);
shidu = (TextView) findViewById(R.id.nowshidu);
pm = (TextView) findViewById(R.id.pm_number);
pmlevel = (TextView) findViewById(R.id.pm_level);
week = (TextView) findViewById(R.id.week);
range = (TextView) findViewById(R.id.range);
tianqi = (TextView) findViewById(R.id.tianqi);
feng = (TextView) findViewById(R.id.feng);
city.setText("N/A");
time.setText("N/A");
wendu.setText("N/A");
shidu.setText("N/A");
pm.setText("N/A");
pmlevel.setText("N/A");
week.setText("N/A");
range.setText("N/A");
tianqi.setText("N/A");
feng.setText("N/A");
// 图片控件初始化
imgpm = (ImageView) findViewById(R.id.imgpm);
imgtianqi = (ImageView) findViewById(R.id.imgtianqi);
// 检查网络连接状况
if (CheckNet.getNetState(this) == CheckNet.NET_NONE) {
Log.d("kkkkkkkkkkkkkkkk", "网络不通!");
Toast.makeText(MainActivity.this, "网络不通!", Toast.LENGTH_LONG).show();
} else {
Log.d("kkkkkkkkkkkkkkkk", "网络OK!");
Toast.makeText(MainActivity.this, "网络OK!", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// 接收返回的citycode数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
String newCityCode = data.getStringExtra("cityCode");
Log.d("kkkkkkkkkk", "选择的城市代码为" + newCityCode);
if (newCityCode != "-1") {
getWeatherDatafromNet(newCityCode);
}
}
}
// 检查网络情况
public static class CheckNet {
public final static int NET_NONE = 0;
public final static int NET_WIFI = 1;
public final static int NET_MOBILE = 2;
public static int getNetState(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null)
return NET_NONE;
int type = networkInfo.getType();
if (type == ConnectivityManager.TYPE_MOBILE)
return NET_MOBILE;
else if (type == ConnectivityManager.TYPE_WIFI)
return NET_WIFI;
return NET_MOBILE;
}
}
// 判断网络连接情况的方法
private void getWeatherDatafromNet(String cityCode) {
final String address = "http://wthrcdn.etouch.cn/WeatherApi?citykey=" + cityCode;
// Log.d("Address:",address);
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection urlConnection = null;
TodayWeather todayWeather = null;
try {
URL url = new URL(address);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(8000);
urlConnection.setReadTimeout(8000);
InputStream in = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String str;
while ((str = reader.readLine()) != null) {
sb.append(str);
// Log.d("kkkkkkkkkkkk",str);
}
String response = sb.toString();
// Log.d("kkkkkkkkkkkk",response);
todayWeather = parseXML(response);
if (todayWeather != null) {
// Log.d("kkkkkkkkkkkk", todayWeather.toString());
Message msg = new Message();
msg.what = UPDATE_TODAY_WEATHER;
msg.obj = todayWeather;
mHandler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
// 刷新天气情况按钮点击事件
@Override
public void onClick(View v) {
if (v.getId() == R.id.update_image) {
// 调用getWeatherDatafromNet方法获取天气信息
getWeatherDatafromNet("101200401");
}
if (v.getId() == R.id.city_manager) {
Intent i = new Intent(this, CityManager.class);
startActivityForResult(i, 1);
}
}
// 解析xml方法
private TodayWeather parseXML(String xmlData) {
TodayWeather todayweather = null;
int fengliCount = 0;
int fengxiangCount = 0;
int dateCount = 0;
int highCount = 0;
int lowCount = 0;
int typeCount = 0;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xmlData));
int eventType = xmlPullParser.getEventType();
// Log.d("kkkkkkkkkkkkkkkk", "start parse xml");
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
// 文档开始位置
case XmlPullParser.START_DOCUMENT:
// Log.d("kkkkkkkkkkkkkkkk", "start doc");
break;
// 标签元素开始位置
case XmlPullParser.START_TAG:
if (xmlPullParser.getName().equals("resp")) {
todayweather = new TodayWeather();
}
if (todayweather != null) {
if (xmlPullParser.getName().equals("city")) {
eventType = xmlPullParser.next();
// Log.d("city", xmlPullParser.getText());
todayweather.setCity(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("updatetime")) {
eventType = xmlPullParser.next();
// Log.d("updatetime", xmlPullParser.getText());
todayweather.setUpdatetime(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("wendu")) {
eventType = xmlPullParser.next();
// Log.d("wendu", xmlPullParser.getText());
todayweather.setWendu(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("fengli") && fengliCount == 0) {
eventType = xmlPullParser.next();
// Log.d("fengli", xmlPullParser.getText());
todayweather.setFengli(xmlPullParser.getText());
fengliCount++;
} else if (xmlPullParser.getName().equals("shidu")) {
eventType = xmlPullParser.next();
// Log.d("shidu", xmlPullParser.getText());
todayweather.setShidu(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("fengxiang") && fengxiangCount == 0) {
eventType = xmlPullParser.next();
// Log.d("fengxiang", xmlPullParser.getText());
fengxiangCount++;
todayweather.setFengxiang(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("pm25")) {
eventType = xmlPullParser.next();
// Log.d("pm25", xmlPullParser.getText());
todayweather.setPm25(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("quality")) {
eventType = xmlPullParser.next();
// Log.d("quelity", xmlPullParser.getText());
todayweather.setQuality(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("date") && dateCount == 0) {
eventType = xmlPullParser.next();
// Log.d("date", xmlPullParser.getText());
dateCount++;
todayweather.setDate(xmlPullParser.getText());
} else if (xmlPullParser.getName().equals("high") && highCount == 0) {
eventType = xmlPullParser.next();
// Log.d("high", xmlPullParser.getText());
highCount++;
todayweather.setHigh(xmlPullParser.getText().substring(2).trim());
} else if (xmlPullParser.getName().equals("low") && lowCount == 0) {
eventType = xmlPullParser.next();
// Log.d("low", xmlPullParser.getText());
lowCount++;
todayweather.setLow(xmlPullParser.getText().substring(2).trim());
} else if (xmlPullParser.getName().equals("type") && typeCount == 0) {
eventType = xmlPullParser.next();
// Log.d("type", xmlPullParser.getText());
typeCount++;
todayweather.setType(xmlPullParser.getText());
}
}
break;
// 判断当前事件是否为标签元素结束事件
case XmlPullParser.END_TAG:
break;
}
eventType = xmlPullParser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return todayweather;
}
// 利用TodayWeather对象更新UI中的控件
void updateTodayWeather(TodayWeather todayWeather) {
city.setText(todayWeather.getCity());
time.setText(todayWeather.getUpdatetime() + "发布");
wendu.setText("当前温度:" + todayWeather.getWendu() + "°C");
shidu.setText("当前湿度:" + todayWeather.getShidu());
pm.setText(todayWeather.getPm25());
pmlevel.setText("空气质量:" + todayWeather.getQuality());
week.setText(todayWeather.getDate());
range.setText(todayWeather.getLow() + "~" + todayWeather.getHigh());
tianqi.setText(todayWeather.getType());
feng.setText(todayWeather.getFengxiang() + todayWeather.getFengli());
if (todayWeather.getPm25() != null) {
int pm25 = Integer.parseInt(todayWeather.getPm25());
if (pm25 <= 50) {
imgpm.setImageResource(R.drawable.pm_0_50);
} else if (pm25 >= 51 && pm25 <= 100) {
imgpm.setImageResource(R.drawable.pm_51_100);
} else if (pm25 >= 101 && pm25 <= 150) {
imgpm.setImageResource(R.drawable.pm_101_150);
} else if (pm25 >= 151 && pm25 <= 200) {
imgpm.setImageResource(R.drawable.pm_151_200);
} else if (pm25 >= 201 && pm25 <= 300) {
imgpm.setImageResource(R.drawable.pm_201_300);
} else if (pm25 > 300) {
imgpm.setImageResource(R.drawable.pm_300);
}
}
// if (todayWeather.getType() != null) {
// switch (todayWeather.getType()) {
// case "晴":
// imgtianqi.setImageResource(R.drawable.qing);
// break;
// case "阴":
// imgtianqi.setImageResource(R.drawable.yin);
// break;
// case "雾":
// imgtianqi.setImageResource(R.drawable.wu);
// break;
// case "多云":
// imgtianqi.setImageResource(R.drawable.duoyun);
// break;
// case "小雨":
// imgtianqi.setImageResource(R.drawable.xiaoyu);
// break;
// case "中雨":
// imgtianqi.setImageResource(R.drawable.zhongyu);
// break;
// case "大雨":
// imgtianqi.setImageResource(R.drawable.dayu);
// break;
// case "阵雨":
// imgtianqi.setImageResource(R.drawable.zhenyu);
// break;
// case "雷阵雨":
// imgtianqi.setImageResource(R.drawable.leizhenyu);
// break;
// case "雷阵雨冰雹":
// imgtianqi.setImageResource(R.drawable.leizhenyubingbao);
// break;
// case "暴雨":
// imgtianqi.setImageResource(R.drawable.baoyu);
// break;
// case "大暴雨":
// imgtianqi.setImageResource(R.drawable.dabaoyu);
// break;
// case "特大暴雨":
// imgtianqi.setImageResource(R.drawable.tedabaoyu);
// break;
// case "阵雪":
// imgtianqi.setImageResource(R.drawable.zhenxue);
// break;
// case "暴雪":
// imgtianqi.setImageResource(R.drawable.baoxue);
// break;
// case "大雪":
// imgtianqi.setImageResource(R.drawable.daxue);
// break;
// case "小雪":
// imgtianqi.setImageResource(R.drawable.xiaoxue);
// break;
// case "雨夹雪":
// imgtianqi.setImageResource(R.drawable.yujiaxue);
// break;
// case "中雪":
// imgtianqi.setImageResource(R.drawable.zhongxue);
// break;
// case "沙尘暴":
// imgtianqi.setImageResource(R.drawable.shachenbao);
// break;
// default:
// break;
// }
// }
Toast.makeText(MainActivity.this, "更新成功!", Toast.LENGTH_SHORT).show();
}
}
以上是对整个天气预报项目的开发的解释和架构说明,如果你觉得写的不错,适合学习,欢迎大家下载源码,交流学习;
兄弟们,微信扫一扫
项目全部源码地址: 点击打开链接,欢迎下载交流学习 天南地北,点赞支持一波...