技术要点及实现方法:
1.Json数据的获取--HttpUrlConnection或xUtils第三方类库
2.Json数据的解析--Gson第三方类库
3.更新UI的三种方式--runOnUIThread、post、handleMessage
4.数据展示--RecyclerView
5.菜单栏切换ListView、GridView、瀑布流效果
准备工作:
1.需要Json数据,数据来源:聚合天气城市的接口数据,下载地址:http://pan.baidu.com/s/1kVc2mz1,提取码:7wpj,将json文件放置在apache-tomcat-7.0.42\webapps\ROOT目录下
2.电脑上需要搭建tomcat服务器(若有接口调用,不需tomcat服务器,但是个人建议,搭建一个服务器有利于个人开发。有了服务器,可以进行很多网络操作,如数据的上传、下载、软件更新等等功能)
tomcat服务器下载地址:http://download.youkuaiyun.com/detail/tideseng/9670507,无需安装,解压即可,具体使用方法请看简介
3.在AndroidStudio中导入RecyclerView的v7包
4.在AndroidStudio中导入Gson架包
5.在AndroidStudio中导入xutils架包,在app目录下的build.gradle里加入:useLibrary 'org.apache.http.legacy'
注意:
1.访问网络需要加Intent权限
2.访问服务器需要开启tomcat服务器,双击“startup.bat“,关闭则双击"shutdown.bat"
先看实现效果图:
实现代码:
activity的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/yin_bg"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:text="添加城市"/>
<EditText
android:id="@+id/et_add_city"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/editview_shape"
android:drawableLeft="@drawable/sousuo"
android:ems="12"
android:hin