因为项目中需要用到天气预报功能,所以就自己研究了下,
欢迎转载请加地址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="&