天气预报的简单制作(二)

来源:互联网 发布:vbs 读取网页源码 编辑:程序博客网 时间:2024/06/03 19:01

 承接上文天气预报的简单制作(一)


4、设置权限

<uses-permission android:name="android.permission.INTERNET"/>

5、显示天气信息(实体类)

定义GSON实体类(basic、aqi、now、suggestion和daily_forecast)在gson中建立basic、aqi、suggestion、now和forecast。并新建weather来对这些进行引用。

(1)为什么借助GSON来对天气信息进行解析而不用JSONObject?

因为和风天气中返回的JSON数据结构非常复杂,如果用JSONObject来解析就会非常麻烦。

(2)在这里用得上key值,参数中传来的天气id和我们之前申请的key值拼装成一个接口地址并调用解析数据。

6、手动更新天气和切换城市

(1)下拉刷新菜单

在写好的布局代码中加入SwipeRefreshLayout,再在主函数中加入更新天气的处理逻辑这样就可以了,很简单吧!!

<?xml version="1.0" encoding="utf-8"?><!--将先前的天气界面各部分的布局文件引入--><FrameLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/colorPrimary">    <ImageView        android:id="@+id/bing_pic_img"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop" />    <android.support.v4.widget.DrawerLayout        android:id="@+id/drawer_layout"        android:layout_width="match_parent"        android:layout_height="match_parent">        <!--在ScrollView在嵌套一个SwipeRefreshLayout就可以下拉刷新-->        <android.support.v4.widget.SwipeRefreshLayout            android:id="@+id/swipe_refresh"            android:layout_width="match_parent"            android:layout_height="match_parent">            <ScrollView                android:id="@+id/weather_layout"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:scrollbars="none"                android:overScrollMode="never">                <LinearLayout                    android:orientation="vertical"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:fitsSystemWindows="true">                    <include layout="@layout/title" />                    <include layout="@layout/now" />                    <include layout="@layout/forecast" />                    <include layout="@layout/aqi" />                    <include layout="@layout/suggestion" />                </LinearLayout>            </ScrollView>        </android.support.v4.widget.SwipeRefreshLayout>        <fragment            android:id="@+id/choose_area_fragment"            android:name="com.coolweather.android.ChooseAreaFragment"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_gravity="start"            />    </android.support.v4.widget.DrawerLayout></FrameLayout>
(2)切换城市

在之前遍历全国省市县的数据完成的前提下只需要在天气布局中引用即可,这样就可以快速切换城市的功能。

(3)后台自动更新天气

要想实现这个功能就需要创建一个长期在后台运行的定时任务。

7、修改图标和名称并仔细检查代码

这就按照你的喜好,这就完成了。

原创粉丝点击