前面一篇文章介绍了如何使用ksoap获取天气信息,但是使用的网络资源受到了限制,所以我们这里会采用第二种方法,可以无限制的获取。http://m.weather.com.cn/data/101010100.html 但是对应的101010100(北京)我们怎么获取呢,还有就是图片资源怎么来的呢?http://m.weather.com.cn/img/b1.gif这个是图片资源,但是每次从网上去还是比较费流量的,我仔细对比了Ksoap中给的gif图片资源,和中国气象局的这个图片都是一一对应的,所以这里我会做成本地图片。

{
"weatherinfo":{
<!-- 基本信息 -->
"city":"北京",
"city_en":"北京",
"date_y":"2013年5月14日",
"date":"",
"week":"星期一",
"fchh":"08",
"cityid":"101010100",
<!-- 从今天开始到第六天的每天的天气情况,这里的温度是摄氏温度 -->
"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃",
<!-- 从今天开始到第六天的每天的天气情况,这里的温度是华氏温度 -->
"tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",
<!-- 天气描述 -->
"weather1":"阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨转阵雨","weather6":"阵雨转小雨",
<!-- 天气描述图片序号 -->
"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","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":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单裤。",
<!-- 48小时穿衣指数 -->
"index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
<!-- 紫外线及48小时紫外线 -->
"index_uv":"弱","index48_uv":"最弱",
<!-- 洗车 -->
"index_xc":"不宜",
<!-- 旅游 -->
"index_tr":"适宜",、
<!-- 舒适指数 -->
"index_co":"较不舒适",
"st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",
<!-- 晨练 -->
"index_cl":"较不宜",
<!-- 晾晒 -->
"index_ls":"不太适宜",
<!-- 过敏 -->
"index_ag":"不易发"
}
}

下面我主要讲下程序:

1.1城市代码获取

这里我把下载下来的城市代码的空行给去掉了,把文件保存为txt格式(UTF-8另存为可以看见)。下载地址:http://download.csdn.net/detail/feiyangxiaomi/6261685程序中的读取方法为:

/***************************************************************************************
* 注意在读入txt的时候是UTF-8,自己看好自己的txt文本格式,在另存为就可以看出来。
*/
private Map<String,String> cityCodes; //根据城市信息索引自己的code
private List<String> citys; //给城市做数据源
private void getAssetsContent(){
try {
String buf;
citys = new ArrayList<String>();
cityCodes = new HashMap<String, String>();
InputStream input = this.getAssets().open("cityCode.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(input,"UTF-8"));
while((buf = br.readLine())!=null){
String[] codeCity = buf.split("=");
citys.add(codeCity[1]);
cityCodes.put(codeCity[1], codeCity[0]);
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i(TAG, e.toString());
e.printStackTrace();
}
}

在使用的时候直接索引对应的城市即可。文件夹放在assets目录下,为不受编译才部分。

1.2网络数据的使用

private void refreshUI(JSONObject jsonobject){

	JSONObject jsonData = jsonobject;
try
{
TextView today_text = (TextView) findViewById(R.id.today);
today_text.setText(jsonData.getString("date_y")); TextView city_text = (TextView) findViewById(R.id.city_text);
city_text.setText(jsonData.getString("city")); TextView today_weather = (TextView) findViewById(R.id.today_weather);
today_weather.setText(jsonData.getString("weather1")); // 取得<string>15℃/21℃</string>中的数据
TextView qiweng_text = (TextView) findViewById(R.id.qiweng);
qiweng_text.setText(jsonData.getString("temp1")); // 取得<string>今日天气风速情况
TextView shidu_text = (TextView) findViewById(R.id.shidu);
shidu_text.setText(jsonData.getString("wind1")); // 取得<string>东北风3-4级</string>中的数据
TextView fengli_text = (TextView) findViewById(R.id.fengli);
fengli_text.setText(jsonData.getString("fl1")); // 取得<string>舒适指数和紫外线强度
TextView kongqi_text = (TextView) findViewById(R.id.kongqi);
kongqi_text.setText(jsonData.getString("index_co")); TextView zhiwai_text = (TextView) findViewById(R.id.zhiwai);
zhiwai_text.setText(jsonData.getString("index_uv")); // 设置小贴士数据
TextView xiaotieshi_text = (TextView) findViewById(R.id.xiaotieshi);
xiaotieshi_text.setText("今日小贴士:"+jsonData.getString("index_d")); // 设置当日图片
ImageView image = (ImageView) findViewById(R.id.imageView1);
int icon = parseIcon(jsonData.getString("img1")+".gif");
image.setImageResource(icon); // 取得第二天的天气情况
TextView tomorrow_date = (TextView) findViewById(R.id.tomorrow_date);
tomorrow_date.setText(jsonData.getString("weather2")); TextView tomorrow_qiweng = (TextView) findViewById(R.id.tomorrow_qiweng);
tomorrow_qiweng.setText(jsonData.getString("temp2")); TextView tomorrow_tianqi = (TextView) findViewById(R.id.tomorrow_tianqi);
tomorrow_tianqi.setText(jsonData.getString("wind2")); ImageView tomorrow_image = (ImageView) findViewById(R.id.tomorrow_image);
int icon1 = parseIcon(jsonData.getString("img3")+".gif");
tomorrow_image.setImageResource(icon1); // 取得第三天的天气情况
TextView afterday_date = (TextView) findViewById(R.id.afterday_date);
afterday_date.setText(jsonData.getString("weather3")); TextView afterday_qiweng = (TextView) findViewById(R.id.afterday_qiweng);
afterday_qiweng.setText(jsonData.getString("temp3")); TextView afterday_tianqi = (TextView) findViewById(R.id.afterday_tianqi);
afterday_tianqi.setText(jsonData.getString("wind3")); ImageView afterday_image = (ImageView) findViewById(R.id.afterday_image);
int icon2 = parseIcon(jsonData.getString("img5")+".gif");
afterday_image.setImageResource(icon2); // 取得第四天的天气情况
TextView nextday_date = (TextView) findViewById(R.id.nextday_date);
nextday_date.setText(jsonData.getString("weather4")); TextView nextday_qiweng = (TextView) findViewById(R.id.nextday_qiweng);
nextday_qiweng.setText(jsonData.getString("temp4")); TextView nextday_tianqi = (TextView) findViewById(R.id.nextday_tianqi);
nextday_tianqi.setText(jsonData.getString("wind4")); ImageView nextday_image = (ImageView) findViewById(R.id.nextday_image);
int icon3 = parseIcon(jsonData.getString("img7")+".gif");
nextday_image.setImageResource(icon3);
}catch(Exception e){
e.printStackTrace();
} }

这里我们直接获取网络上的JSON数据,把数据放入对应的位置即可,图片资源的使用方法不变,还是放在本地drawalbe文件下。

1.3图片资源的使用

// 工具方法,该方法负责把返回的天气图标字符串,转换为程序的图片资源ID。
private int parseIcon(String strIcon)
{
if (strIcon == null)
return -1;
if ("0.gif".equals(strIcon))
return R.drawable.a_0;
if ("1.gif".equals(strIcon))
return R.drawable.a_1;
if ("2.gif".equals(strIcon))
return R.drawable.a_2;
if ("3.gif".equals(strIcon))
return R.drawable.a_3;

……

这里就不全部贴上了。

1.4最重要的一件事情

(1)源码http://download.csdn.net/detail/feiyangxiaomi/6261805(2)资源(源码里面有)

android天气查询(二)之网络json数据的获取的更多相关文章

  1. Ace教你一步一步做Android新闻客户端(三) JSON数据解析

    对于服务器端来说,返回给客户端的数据格式一般分为html.xml和json这三种格式,现在给大家讲解一下json这个知识点, 1 如何通过json-lib和gson这两个json解析库来对解析我们的j ...

  2. 模拟QQ侧滑控件 实现三种界面切换效果(知识点:回调机制,解析网络json数据,fragment用法等)。

    需要用到的lib包 :解析json  gson包,从网络地址解析json数据成String字符串的异步网络解析工具AsyncHttpClient等 下载地址:点击下载 Xlistview 下拉上拉第三 ...

  3. 解析网络json数据,模拟美团界面显示。

    <?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android=&q ...

  4. Android客户端与服务器之间传递json数据

    在服务器与客户端之间通信,json数据是一种常用格式,本文主要在服务器端构建数据,在客户端接收显示,并且在listview上显示出来 服务器端的构建 简单的javabean与返回结果函数与插入函数略过 ...

  5. android 初步了解应用Gson 解析Json数据

    1,因为没有服务器返回数据,对于Tomcat又懒得去配,所以我直接把数据写死到app中 先写一个实体类,便于操作 /** * 实体类 */ public class Person { int id ; ...

  6. pyspider示例代码二:解析JSON数据

    本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下 ...

  7. 如何构建JSON数据,JSON数据的格式,JSON数据的获取

    假设你是用$.getJSON();方法获取JSON数据$.getJSON(url,{"Action":"getStudent"},function(data){ ...

  8. ODAC (V9.5.15) 学习笔记(二十)大数据量获取处理

    ODAC获取数据的效率比较高,在Web程序中希望能够更快获取第一页的数据时,可以有几种方式: 1.在数据库中进行分页处理: 2.获取所有数据,只是快速返回第一页数据. 第一种方案对应用服务器资源消耗最 ...

  9. android解析网络json数据(1)

    1.首先获得url,传入URL类,利用URL的openconnection方法,获得URLConnection,去的输入流,进行操作,具体代码如下: public class NetConnectio ...

随机推荐

  1. api文档生成工具 C#

    要为编写的程序编写文档,这是件费力气的事,如果能自动生成就好了. 不怕做不到,就怕想不到.这不,搜索到了Sandcastle 比较好的参考文章: 1.Sandcastle官方网址: http://sh ...

  2. socket(套接字)

    客户端: 创建套接字(socket) 连接服务器(connect) 通信(send,recv或者write,read) 关闭套接字(closesocket) 示例代码: int main(int ar ...

  3. BHO启动IE调试

    如下图选择Web Browser Debugger, 输入启动网址

  4. TOJ 1702.A Knight's Journey

    2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...

  5. 数据库分页【Limt与Limt..OFFSET 】

    数据起始 SELECT * from xiaoyao_blogs_essay  limit 20 , 15;解释:20是起始位置,15是页容量.因为id是从15开始的 SELECT * from xi ...

  6. 用Cython加速Python程序以及包装C程序简单测试

    用Cython加速Python程序 我没有拼错,就是Cython,C+Python=Cython! 我们来看看Cython的威力,先运行下边的程序: import time def fib(n): i ...

  7. 《Pointers On C》读书笔记(第二章 基本概念)

    1.从源代码到生成可执行程序的过程整体上可以分为两个阶段:编译和链接.其中,编译过程大致上又可分为:预处理.编译和汇编.预处理阶段主要对源代码中的预处理指令(包含宏定义指令<如 #define& ...

  8. 用NDKr9编译最新ffmpeg2.0.1到android平台

    原文来自http://www.mingjianhua.com 本文参照 http://www.roman10.net/how-to-build-ffmpeg-with-ndk-r9/ 在linux下的 ...

  9. (Android) ContentProvider 实例

    ContentProvider 用于应用程序(Android Application)之间传递数据,包括Insert, update, delete, query. 下面的例子是在两个应用之间传递数据 ...

  10. 飘逸的python - 有的升序有的降序的情况下怎么多条件排序

    之前在统计导出各区服玩家消费的时候需要进行升序降序混搭的多条件排序. 需求是这样的.区服从小到大排,如果区服相同,则按消费从大到小排. 实现方法是利用python的sort算法是稳定排序,对数据进行多 ...