前面一篇文章介绍了如何使用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)资源(源码里面有)
分享到:
相关推荐
在模块对应的包名下创建JSON数据对应的实体类WeatherInfo类,实体类中的成员名称要与JSON数组的key值一致,并为每个属性添加get和set方法。public String getTemp() {return temp;}public void setTemp(String temp)...
该APP的设计与实现需要结合Android平台的特点和JSON数据交换格式,以实现天气预报信息的获取、解析和呈现。 一、Android平台下的天气预报APP设计 Android平台下的天气预报APP设计需要考虑到Android系统的特点,如...
android天气预报通过网络读取数据并解析JSON。需要用到JSONObject
android 天气预报 通过获取固定网络地址的JSON数据,并返回读取该数据展示到视图组件上
通过获取百度的天气API,获取百度的json数据,可以获取最近几天的天气(也可以获取别的API接口),获取之后我把这些天气解析然后通过textview和imageview展示出来。
Android中基于心知天气获取天气信息JSONJSON简介JSON对象JSON数组JSON解析Android中获取天气获取天气的流程获取心知天气的API key获取心知天气的API代码部分实现的效果如下进阶-获取任意城市的天气信息 为什么要写这...
volley使用演示,解决中文乱码问题。 获取json格式天气信息,使用JSONObject解析数据
通过获取百度的天气API,获取百度的json数据,可以获取最近几天的天气(也可以获取别的API接口),获取之后我把这些天气解析然后通过textview和imageview展示出来。
Android天气预报的制作,其中用到了okhttp访问网络以及用到Gosn解析json数据。
天气获取解析数据和数据库等等,JSON
通过安卓网络编程的方式可以很方便的获取到天气json数据,通过使用GSON库,解析为我们软件需要的数据结构,给软件的编写提供了极大的便利。安卓的Activity、Fragment、Service等提供了强大的数据处理功能,使各个...
中国天气网的api,http获取网络信息,json解析数据,显示到界面。数据库存放城市与城市的url,可以进行收藏,切换城市天气。 Android Studio实现天气预报App,满分课设大作业项目源码.zip代码完整下载可用,确保...
获取经纬度,然后使用百度的JAR包获取城市名称,使用httpClient获取天气的JSON数据,并进行解析,并显示。
android天气预报demo,从中国天气网获取xml数据和json数据。解析xml文件和json数据。
获取到天气的json数据后把所有数据保存到本地的文件里,然后再在本地文件中读取数据显示到界面上,PM2.5数据由pm2d5.com提供,获取方式与天气获取方式雷同。定位是直接用的系统的定位,我在手机上测试的是现在的项目...
android 天气预报,通过json解析获取天气数据,不过气象局的api已经不更新了,可以当学习用
本个人理财软件系统是在Android移动平台的基础上开发,提供了预算管理、收支管理、明细管理、报表查询等基本功能,同时还为用户提供了简易计算、记事备忘、天气查询、股票信息、中英互译、手机信息查询等实用功能,...
主要介绍了Android天气预报之基于HttpGet对象解析天气数据的方法,非常实用的功能,需要的朋友可以参考下
基于android的天气预报软件,动态的从网络上获取所选地区的天气情况相关信息,通过json数据格式返回本地,进行解析,呈现