HttpURLConnection+JSON的使用实例

这篇博客介绍了如何使用HttpURLConnection结合JSON来获取天气预报数据。JSON作为轻量级的数据交换格式,常用于APP开发中的数据交互。文章详细讲解了JSON的JSONObject和JSONArray两种数据格式,并提供了具体的代码示例,包括XML布局、Activity代码以及weatherTask类的实现,展示了如何从服务器获取并解析天气预报信息。

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

JSON

JSON是一种轻量级的数据交换格式,主要用于跟服务器进行交换数据。
JSON数据格式应用非常广泛,在APP开发中,大多数据端返回的接口数据都是采用JSON数据格式的。

JSON的两种数据格式

JSONObject和JSONArray,JSONObject表示JSON对象,而JSONArray表示的则是数组。

运用HttpURLConnection+JSON获取天气预报数据信息

代码如下
Activity中xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.lenovo.liu.weatherdemo.MainActivity">

    <EditText
        android:id="@+id/weath_city"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="请输入所要查询的城市" />

    <Button
        android:id="@+id/weather_btn"
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:text="查询" />

    <TextView
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值