本篇完成的任务主要是在我们选定城市后,显示该城市的天气。
在第三篇中,我们提到的工具类Utility里有一个方法叫做handleWeatherResponse,它利用json解析天气数据的;还有一个方法叫做saveWeatherInfo,它利用SharedPreferences来保存天气数据的,具体的请去第三篇中去看。网址:http://blog.csdn.net/guya1990/article/details/42711171
先看看显示天气的ui
它的代码为:
第一个RelativeLayout里显示的是标题和两个按钮,两个按钮分别是刷新和重新选择城市。
第二个RelativeLayout里就是利用textView把我们json解析的内容放进去。
然后去实现逻辑代码。创建一个类:WeatherActivity,然后去清单文件里去注册一下。
在这个类中,我们主要完成以下几个任务。
mission1:
初始化ui代码里的控件
init();
private void init() {
weatherInfoLayout = (LinearLayout) findViewById(R.id.weather_info_layout);
cityNameText = (TextView) findViewById(R.id.city_name);
publishText = (TextView) findViewById(R.id.publish_text);
weatherDespText = (TextView) findViewById(R.id.weather_desp);
temp1Text = (TextView) findViewById(R.id.temp1);
temp2Text = (TextView) findViewById(R.id.temp2);
currentDateText = (TextView) findViewById(R.id.current_date);
switchCity = (Button) findViewById(R.id.switch_city);
refreshWeather = (Button) findViewById(R.id.refresh_weather);
}
获取天气代码
/**
* 查询县级代号对应的天气代号
*
* @param countyCode
*/
private void queryWeatherCode(String countyCode) {
String address = "http://www.weather.com.cn/data/list3/city"
+ countyCode + ".xml";
queryFromServer(address, "countyCode");
}
获取联网地址,然后去调用queryFromServer()方法去查询县级天气代号。查询完后会回调queryWeatherInfo()方法。
mission3:
根据天气代码去查询天气
/**
* 查询天气代号对应的天气
*
* @param weatherCode
*/
private void queryWeatherInfo(String weatherCode) {
String address = "http://www.weather.com.cn/data/cityinfo/"
+ weatherCode + ".html";
queryFromServer(address, "weatherCode");
}
private void queryFromServer(final String address, final String type) {
HttpUtil.sendRequest(address, new HttpCallbackListener() {
@Override
public void onFinish(final String response) {
if (type.equals("countyCode")) {
if (!TextUtils.isEmpty(response)) {
// 从服务器返回的数据中解析出天气代号
String[] array = response.split("\\|");
if (array != null && array.length == 2) {
String weatherCode = array[1];
queryWeatherInfo(weatherCode);
}
}
} else if (type.equals("weatherCode")) {
// 处理服务器返回的天气信息
Utility.handleWeatherResponse(WeatherActivity.this,
response);
runOnUiThread(new Runnable() {
public void run() {
showWeather();
}
});
}
}
@Override
public void onError(Exception e) {
runOnUiThread(new Runnable() {
public void run() {
publishText.setText("同步失败");
}
});
}
});
}
显示天气
/**
* 从sharedPreferences文件中读取天气信息,并且显示到界面上
*/
private void showWeather() {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
cityNameText.setText(sp.getString("city_name", ""));
temp1Text.setText(sp.getString("temp1", ""));
temp2Text.setText(sp.getString("temp2", ""));
weatherDespText.setText(sp.getString("weather_desp", ""));
publishText.setText("今天" + sp.getString("publish_time", "") + "发布");
currentDateText.setText(sp.getString("current_date", ""));
weatherInfoLayout.setVisibility(View.VISIBLE);
cityNameText.setVisibility(View.VISIBLE);
}
调用本地sp里面的数据,显示天气。
自此,我们就大致描述完了显示天气的逻辑代码。下面给出全部代码。
package org.guya.myweather;
import org.guya.myweather.utils.HttpCallbackListener;
import org.guya.myweather.utils.HttpUtil;
import org.guya.myweather.utils.Utility;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class WeatherActivity extends Activity implements OnClickListener {
private LinearLayout weatherInfoLayout;
/**
* 用于显示城市名字
*/
private TextView cityNameText;
/**
* 用于显示发布时间
*/
private TextView publishText;
/**
* 用于显示天气描述信息
*/
private TextView weatherDespText;
/**
* 用于显示温度1
*/
private TextView temp1Text;
/**
* 用于显示温度2
*/
private TextView temp2Text;
/**
* 用于显示当前日期
*/
private TextView currentDateText;
/**
* 切换城市按钮
*/
private Button switchCity;
/**
* 更新天气按钮
*/
private Button refreshWeather;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.weather_layout);
init();
String countyCode = getIntent().getStringExtra("county_code");
if (!TextUtils.isEmpty(countyCode)) {
// 有县级代号就去查询天气
publishText.setText("同步ing");
weatherInfoLayout.setVisibility(View.INVISIBLE);
cityNameText.setVisibility(View.INVISIBLE);
queryWeatherCode(countyCode);
} else {
// 没有县一级的code,直接查询显示本地天气
showWeather();
}
switchCity.setOnClickListener(this);
refreshWeather.setOnClickListener(this);
}
private void init() {
weatherInfoLayout = (LinearLayout) findViewById(R.id.weather_info_layout);
cityNameText = (TextView) findViewById(R.id.city_name);
publishText = (TextView) findViewById(R.id.publish_text);
weatherDespText = (TextView) findViewById(R.id.weather_desp);
temp1Text = (TextView) findViewById(R.id.temp1);
temp2Text = (TextView) findViewById(R.id.temp2);
currentDateText = (TextView) findViewById(R.id.current_date);
switchCity = (Button) findViewById(R.id.switch_city);
refreshWeather = (Button) findViewById(R.id.refresh_weather);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.switch_city:
Intent intent = new Intent(this, ChooseAreaActivity.class);
intent.putExtra("from_weather_activity", true);
startActivity(intent);
finish();
break;
case R.id.refresh_weather:
publishText.setText("同步ing...");
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
String weatherCode = sp.getString("weather_code", "");
if (!TextUtils.isEmpty(weatherCode)) {
queryWeatherInfo(weatherCode);
}
break;
default:
break;
}
}
/**
* 查询县级代号对应的天气代号
*
* @param countyCode
*/
private void queryWeatherCode(String countyCode) {
String address = "http://www.weather.com.cn/data/list3/city"
+ countyCode + ".xml";
queryFromServer(address, "countyCode");
}
/**
* 查询天气代号对应的天气
*
* @param weatherCode
*/
private void queryWeatherInfo(String weatherCode) {
String address = "http://www.weather.com.cn/data/cityinfo/"
+ weatherCode + ".html";
queryFromServer(address, "weatherCode");
}
private void queryFromServer(final String address, final String type) {
HttpUtil.sendRequest(address, new HttpCallbackListener() {
@Override
public void onFinish(final String response) {
if (type.equals("countyCode")) {
if (!TextUtils.isEmpty(response)) {
// 从服务器返回的数据中解析出天气代号
String[] array = response.split("\\|");
if (array != null && array.length == 2) {
String weatherCode = array[1];
queryWeatherInfo(weatherCode);
}
}
} else if (type.equals("weatherCode")) {
// 处理服务器返回的天气信息
Utility.handleWeatherResponse(WeatherActivity.this,
response);
runOnUiThread(new Runnable() {
public void run() {
showWeather();
}
});
}
}
@Override
public void onError(Exception e) {
runOnUiThread(new Runnable() {
public void run() {
publishText.setText("同步失败");
}
});
}
});
}
/**
* 从sharedPreferences文件中读取天气信息,并且显示到界面上
*/
private void showWeather() {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
cityNameText.setText(sp.getString("city_name", ""));
temp1Text.setText(sp.getString("temp1", ""));
temp2Text.setText(sp.getString("temp2", ""));
weatherDespText.setText(sp.getString("weather_desp", ""));
publishText.setText("今天" + sp.getString("publish_time", "") + "发布");
currentDateText.setText(sp.getString("current_date", ""));
weatherInfoLayout.setVisibility(View.VISIBLE);
cityNameText.setVisibility(View.VISIBLE);
}
}
分2个部分
1,如果之前,我们已经选择过了城市,那么就直接跳转到天气页面
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
if (sp.getBoolean("city_selected", false)) {
Intent intent = new Intent(this, WeatherActivity.class);
startActivity(intent);
finish();
return;
}
2,如果已经选择到了县级,那么就跳转到天气页面
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View view, int index,
long arg3) {
if (currentLevel == LEVEL_PROVINCE) {
selectedProvince = provinceList.get(index);
queryCities();
} else if (currentLevel == LEVEL_CITY) {
selectedCity = cityList.get(index);
queryCounties();
} else if (currentLevel == LEVEL_COUNTY) {
String countyCode = countyList.get(index).getCountyCode();
Intent intent = new Intent(ChooseAreaActivity.this,
WeatherActivity.class);
intent.putExtra("county_code", countyCode);
startActivity(intent);
finish();
}
}
});