Android 根据城市获取天气(天气预报)

本文介绍了如何在Android项目中实现根据城市名称获取天气预报功能。通过分析中国天气网的接口,找到了可用的API(http://m.weather.com.cn/atad/101070101.html)。文章详细讲解了从获取城市ID到解析数据、展示天气信息的主要代码实现,包括布局文件、ListView显示、MainActivity、Weather对象和查询接口等部分。别忘了在AndroidManifest.xml中添加网络权限。
摘要由CSDN通过智能技术生成

因为项目中需要用到天气预报功能,所以就自己研究了下,

尴尬欢迎转载请加地址http://blog.csdn.net/jing110fei/article/details/38515275

首先得找到一个能用的接口,网上出现频率最高的接口是

http://m.weather.com.cn/data/101070101.html  其中101070101就是城市对应的ID

但是完成后,发现此接口2014年3月4日以后就不再更新了快哭了

然后又开始寻找,最后还是中国天气网的另一个接口http://m.weather.com.cn/atad/101070101.html 这个是可以用的。

找到可以用的接口,基本就松口气了,接下来就是取数据的事了。

怎么取数据呢,我们一般是输入一个城市名字,期望得到它未来三天的天气状况,

看上面的接口就知道我们需要解决另一个问题,就是城市对应的ID,我也很抓狂,幸好在网上有前辈整理了基本全部的城市ID。。PS:不得不在这里给这位前辈点个赞!

如上图,将此文件放在了assets文件夹下,好了废话不说,贴出主要代码。

主文件的布局文件activity_weather_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
	<LinearLayout 
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:orientation="horizontal">
	 	<EditText 
	 	    android:id="@+id/city_name"
	 	    android:layout_width="0dp"
	 	    android:layout_height="wrap_content"
	 	    android:hint="请输入城市名字"
	 	    android:layout_weight="1"/>
	 	<Button 
        	android:id="@+id/query_cityID"
        	android:layout_width="0dp"
    		android:layout_height="wrap_content"
    		android:layout_weight="1"
    		android:text="查询城市ID"
        />
	</LinearLayout>
	<LinearLayout 
	   android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:orientation="horizontal" >
	    <TextView 
	        android:id="@+id/city_id"
	 	    android:layout_width="0dp"
	 	    android:layout_height="wrap_content"
	 	    android:background="&
  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值