android网络数据获取与解析+RecyclerView数据展示实现ListView、GridView、瀑布流切换

该博客介绍了如何在Android中获取Json数据,使用HttpUrlConnection或xUtils,解析数据借助Gson。内容涵盖主线程更新UI的三种方式,通过RecyclerView展示数据,并实现在菜单栏切换ListView、GridView、瀑布流效果。首先,准备Json数据和本地Tomcat服务器,接着导入RecyclerView和Gson库,最后讲解了关键代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

技术要点及实现方法:

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值