<Android 应用 之路> 天气预报(二)
界面组成
- 载入界面
- 显示界面
Activity两个,一个用来显示载入界面,一个用来显示天气信息
主要代码如下:
public class MyActivity extends Activity {
private static final String TAG = "WelcomeActiivty";
private ProgressBar mLoadingBar; //载入进度条
private TextView mVersionName; //载入界面显示版本信息
private AsyncTask mLoadingTask; //后台载入异步任务
private ArrayList<WeatherInfo> weatherInfoList; //用来保存天气信息
//天气信息接口
private static final String frontUrl = "http://www.weather.com.cn/data/cityinfo/";
private static final String suffix = ".html";
//与上面配合使用,中间添加城市code,如
//http://www.weather.com.cn/data/cityinfo/101010100.html
//所有城市的列表
private int[] mCityCodeList = {101010100, 101010300, 101010400, 101010500, 101010600,
101010700, 101010800, 101010900, 101011000, 101011100, 101011200, 101011300,
101011400, 101011500, 101011600, 101011700, 101011800, 101011900, 101012000,
101012100, 101012200, 101010200, 101030100, 101030300, 101030400, 101030500,
101030600, 101031400, 101030800, 101030900, 101031000, 101031100, 101031200,
101030200, 101030700, 101020100, 101020300, 101020500, 101020600, 101021300,
101020800, 101020900, 101021000, 101021100, 101021200, 101020200, 101020700,
101090101, 101090301, 101090402, 101090501, 101091101, 101090701, 101090801,
101090901, 101091001, 101090201, 101090601, 101180101, 101180301, 101180401,
101180501, 101180601, 101180701, 101180801, 101180901, 101181001, 101181101,
101181201, 101181301, 101181401, 101181501, 101181601, 101181701, 101181801,
101180201, 101220101, 101220301, 101220401, 101220501, 101220601, 101220701,
101220801, 101220901, 101221001, 101221101, 101221201, 101221301, 101221401,
101221501, 101221601, 101221701, 101220201, 101210101, 101211101, 101210201,
101210301, 101210901, 101210501, 101210601, 101210701, 101210801, 101211001,
101210401, 101040100, 101040300, 101040400, 101040500, 101040600, 101040700,
101040800, 101040900, 101041000, 101041100, 101041200, 101041300, 101041400,
101041500, 101041600, 101041700, 101041800, 101041900, 101042000, 101042100,
101042200, 101042300, 101042400, 101042500, 101042600, 101042700, 101042800,
101042900, 101043000, 101043100, 101043200, 101043300, 101043400, 101043600,
101043700, 101040200, 101230101, 101230501, 101230601, 101230701, 101230509,
101230901, 101230201, 101230301, 101230401, 101230801, 101160101, 101160301,
101160401, 101160501, 101160601, 101161401, 101160801, 101160901, 101161001,
101161101, 101161201, 101161301, 101160201, 101160701, 101280101, 101280301,
101280401, 101280501, 101280601, 101280701, 101280800, 101280901, 101281001,
101281101, 101281201, 101281301, 101281401, 101281501, 101281601, 101281701,
101281801, 101281901, 101282001, 101282101, 101280201, 101300101, 101300301,
101300401, 101300501, 101300601, 101301401, 101300801, 101300901, 101301001,
101301101, 101301201, 101301301, 101300201, 101300701, 101260101, 101260301,
101260401, 101260906, 101260601, 101260701, 101260801, 101260201, 101260501,
101290101, 101290301, 101290601, 101290701, 101290801, 101290901, 101291001,
101291101, 101291201, 101291301, 101291401, 101291501, 101291601, 101290201,
101290401, 101290501, 101080101, 101080301, 101080401, 101080501, 101081201,
101080701, 101080801, 101080901, 101081000, 101081101, 101080201, 101080601,
101240101, 101240301, 101240401, 101240501, 101241101, 101240701, 101240801,
101240901, 101241001, 101240201, 101240601, 101200101, 101200501, 101200801,
101200901, 101201001, 101201101, 101201201, 101201301, 101201401, 101201501,
101201601, 101201701, 101200201, 101200301, 101200401, 101200601, 101200701,
101270101, 101270301, 101270401, 101270501, 101270601, 101270701, 101270801,
101270901, 101271001, 101271101, 101271201, 101271301, 101271401, 101271501,
101271601, 101271701, 101271801, 101271901, 101272001, 101272101, 101270201,
101170101, 101170501, 101170401, 101170201, 101170301, 101150101, 101150301,
101150801, 101150501, 101150601, 101150701, 101150201, 101150401, 101120101,
101120601, 101120901, 101121001, 101121101, 101121201, 101121301, 101121401,
101121501, 101121601, 101121701, 101120201, 101120301, 101120401, 101120501,
101120701, 101120801, 101110101, 101110300, 101110401, 101111001, 101110601,
101110701, 101110801, 101110901, 101110200, 101110501, 101100101, 101100701,
101100801, 101100901, 101101001, 101100501, 101100201, 101100301, 101100401,
101100601, 101101100, 101130101, 101130301, 101130401, 101130501, 101130601,
101130701, 101130801, 101130901, 101131001, 101131101, 101131201, 101131301,
101131401, 101131501, 101131601, 101130201, 101140101, 101140301, 101140701,
101140501, 101140601, 101140201, 101140401, 101340101, 101340201, 101340401,
101310101, 101310201, 101310202, 101310203, 101310204, 101310205, 101310206,
101310207, 101310208, 101310209, 101310210, 101310211, 101310212, 101310214,
101310215, 101310216, 101310217, 101310220, 101310221, 101310222, 101310102,
101250101, 101250301, 101250401, 101250501, 101250601, 101250700, 101250801,
101250901, 101251001, 101251101, 101251201, 101251301, 101251401, 101251501,
101250201, 101190101, 101190301, 101190401, 101190501, 101190601, 101191301,
101190801, 101190901, 101191001, 101191101, 101191201, 101190201, 101190701,
101050101, 101050301, 101050401, 101050501, 101050601, 101051301, 101050801,
101050901, 101051002, 101051101, 101051201, 101050201, 101050701, 101060101,
101060301, 101060401, 101060901, 101060601, 101060701, 101060801, 101060201,
101060501, 101070101, 101070301, 101070401, 101070501, 101070601, 101071401,
101070801, 101070901, 101071001, 101071101, 101071201, 101071301, 101070201,
101070701};
private int mAvailableLinks = mCityCodeList.length;//用来保存可用的查询结果,后面用来判断载入是否完成
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//没有标题的界面效果
setContentView(R.layout.welcome);
weatherInfoList = new ArrayList<WeatherInfo>();
mLoadingBar = (ProgressBar) findViewById(R.id.loadingbar);
mLoadingBar.setProgress(0);
mLoadingBar.setMax(mCityCodeList.length);//进度条
mVersionName = (TextView) findViewById(R.id.versionname);
mVersionName.setText(getString(R.string.app_name).toString() + " " + getLocalVersionName(this));
final RequestQueue mRequestQueue = Volley.newRequestQueue(this);
AsyncTask mLoadingDataTask = new AsyncTask() {
@Override
protected void onProgressUpdate(Object[] values) {
super.onProgressUpdate(values);
int progress = mLoadingBar.getProgress();
mLoadingBar.setProgress(progress + 1);//更新进度条
if (mLoadingBar.getProgress() == mAvailableLinks) {
showWeatherInfo();
}
}
@Override
protected Object doInBackground(Object[] objects) {
//查询所有的信息并解析出来保存到ArrayList中
for (int i = 0; i < mCityCodeList.length; i++) {
String uri = frontUrl + String.valueOf(mCityCodeList[i]) + suffix;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(uri, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
JSONObject weatherinfojson = jsonObject.getJSONObject("weatherinfo");
WeatherInfo newWeatherInfo = new WeatherInfo(
weatherinfojson.getString("city"),
weatherinfojson.getString("cityid"),
weatherinfojson.getString("temp1"),
weatherinfojson.getString("temp2"),
weatherinfojson.getString("weather"),
weatherinfojson.getString("img1"),
weatherinfojson.getString("img2"),
weatherinfojson.getString("ptime"));
publishProgress();
weatherInfoList.add(newWeatherInfo);
} catch (Exception e) {
mAvailableLinks--;
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
mAvailableLinks--;
Log.e("TAG", volleyError.getMessage(), volleyError);
}
});
//添加到RequestQueue中
mRequestQueue.add(jsonObjectRequest);
}
return null;
}
};
mLoadingDataTask.execute();
}
/**
* 获取本地软件版本名称
*/
public static String getLocalVersionName(Context ctx) {
String localVersion = "";
try {
PackageInfo packageInfo = ctx.getApplicationContext()
.getPackageManager()
.getPackageInfo(ctx.getPackageName(), 0);
localVersion = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return localVersion;
}
//跳转到显示天气信息的界面,并传递List数据
private void showWeatherInfo() {
Bundle bundle = new Bundle();
bundle.putSerializable(Utils.WEATHERINFO, (Serializable) weatherInfoList);
Intent intent = new Intent(MyActivity.this, MainActivity.class);
intent.putExtra(Utils.WEATHERINFO, bundle);
startActivity(intent);
finish();//finish掉这个Activity,否则返回键可以回到载入界面 那就尴尬了!
}
}
Activity之前传递数据的方式 Bundle
public void putAll(Bundle bundle)
public boolean hasFileDescriptors()
public void putByte(String key, byte value)
public void putChar(String key, char value)
public void putShort(String key, short value)
public void putFloat(String key, float value)
public void putCharSequence(String key, CharSequence value)
public void putParcelable(String key, Parcelable value)
public void putSize(String key, Size value)
public void putSizeF(String key, SizeF value)
public void putParcelableArray(String key, Parcelable[] value)
public void putParcelableArrayList(String key, ArrayList< xx extends Parcelable> value)
public void putSparseParcelableArray(String key, SparseArray< xx extends Parcelable> value)
public void putIntegerArrayList(String key, ArrayList value)
public void putStringArrayList(String key, ArrayList value)
public void putCharSequenceArrayList(String key, ArrayList value)
public void putSerializable(String key, Serializable value)
public void putByteArray(String key, byte[] value)
public void putShortArray(String key, short[] value)
public void putCharArray(String key, char[] value)
public void putFloatArray(String key, float[] value)
public void putCharSequenceArray(String key, CharSequence[] value)
public void putBundle(String key, Bundle value)
public void putBinder(String key, IBinder value)
很多类似的方法,Bundle就是一个载体,用什么方式封装后传递,在对应端就用什么方式把它解读出来
通过showWeatherInfo方法跳转到查询到的所有天气的显示界面。下一篇文章讲解。
看下效果图:
<Android 应用 之路> 天气预报(二)的更多相关文章
- <Android 应用 之路> 天气预报(五)
前言 写了上一篇文章,讲了下这个实现天气预报的使用内容,现在又到了看代码的时候,主要还是贴代码,然后添加足够的注释. 聚合数据SDK配置 将juhe_sdk_v_X_X.jar以及armeabi文件夹 ...
- <Android 应用 之路> 天气预报(四)
前言 第二次尝试完成天气预报应用,与上次不同的是,个人感觉这次的Ui不那么丑陋,整体的实用性和界面效果,用户体验相较上一次有所提升,但是还是有很多地方需要完善. 这次使用到的内容比较丰富,包括聚合数据 ...
- <Android 应用 之路> 天气预报(一)
Android天气预报客户端 设计思路 欢迎界面,版本号,应用名 + 数据后台加载(所有城市的信息获取) 数据加载完成后跳转到显示界面,显示所有查询到的城市的天气信息 欢迎界面和天气显示界面分别为单独 ...
- <Android 应用 之路> 天气预报(三)
昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码 基本ListView显示 搜索框,查询城市 上一篇文章中,载入界面通过showWeatherInfo()方法跳转到天气信息显示界面 priv ...
- redis成长之路——(二)
redis操作封装 针对这些常用结构,StackExchange.Redis已经做了一些封装,不过在实际应用场景中还必须添加一些功能,例如重试等 所以对一些常功能做了一些自行封装SERedisOper ...
- Android学习之路书籍推荐
Android开发书籍推荐:从入门到精通系列学习路线书籍介绍 JAVA入门书籍: < Introduction to java programming > < Core java & ...
- Android系列之网络(二)----HTTP请求头与响应头
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android系列之Fragment(二)----Fragment的生命周期和返回栈
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android之自定义生成彩色二维码
先导个zxing.jar包 下面是xml布局 activity_main.xml <RelativeLayout xmlns:android="http://schemas.andro ...
随机推荐
- VijosP1100:加分二叉树
描述 设一个n个节点的二叉树tree的中序遍历为(l,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都有一 ...
- selenium2获取input输入框中的值的三种方法。
- day1 java基础回顾-IO流
IO流的分类 注:这几个类都是抽象类. IO解决问题: 解决设备与设备之间 的数据传输问题. 比如: 硬盘--->内存 内存----->硬盘 字节流: 输入字节流:---------| I ...
- 利用CSS3给图片添加旋转背景特效
首先看旋转特效:http://***/demo/201512/2015-12-09-css3-image-hover-animate/index.html 这是一款纯CSS3实现的当鼠标滑过图片时文字 ...
- JQuery EasyUI validate 扩展
validate 扩展js $.extend($.fn.validatebox.defaults.rules, { equals: { validator: function(value,param) ...
- 用VisualSVN Server创建版本库,以及TortoiseSVN的使用
介绍了VisualSVN Server和TortoiseSVN的下载,安装,汉化. SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. ...
- 2014-11-1 NOIP模拟赛1
冲刺NOIP2014复赛模拟题第六套第二试 题目名称 日历游戏 最大公约数 密码 英文代号 calendar gcd pasuwado 输入文件名 calendar.in gcd.in pasuw ...
- uoj#351. 新年的叶子(概率期望)
传送门 数学还是太差了,想了半天都没想出来 首先有一个定理,如果直径(这里考虑经过的点数)为奇数,所有直径有同一个中点,如果直径为偶数,所有直径有同一条最中间的边.这个可以用反证法,假设不成立的话直径 ...
- C 语言实例 - 输出九九乘法口诀表
C 语言实例 - 输出九九乘法口诀表 使用嵌套 for 循环输出九九乘法口诀表. 实例 #include<stdio.h> int main(){ //外层循环变量,控制行 ; //内层循 ...
- 洛谷P3884 二叉树问题
题目描述 如下图所示的一棵二叉树的深度.宽度及结点间距离分别为: 深度:\(4\) 宽度:\(4\)(同一层最多结点个数) 结点间距离: \(⑧→⑥为8 (3×2+2=8)\) \(⑥→⑦为3 (1× ...