Android开发 和风天气API 解析天气数据

这篇博客介绍了如何申请和风天气开发者账号,并在Android Studio中创建一个天气应用。作者详细阐述了新建Android项目、添加依赖、引用和风天气SDK以及获取API数据的步骤。还展示了创建布局、实体类以及请求天气数据的代码片段。
摘要由CSDN通过智能技术生成

申请和风天气开发者

申请和风天气开发者
申请注册成功后,登录,进入和风天气控制台界面:
在这里插入图片描述

在和风天气控制台新建一个应用

点击“应用管理”并新建一个应用,如图所示:
Alt
然后点击图片上的“添加key”,弹出如下界面:

Alt
输入key名,选择key的类型为Android SDK,Package Name为你创建Android Studio项目的包名,填写完毕之后点击“创建”。
Alt
创建成功后,如图:
Alt
其中的Username和Key是我们要用到的获取和风天气数据的关键。

新建一个Android Studio项目

如图所示:
在这里插入图片描述
注意,新建的项目的包名要和和风天气新建应用添加的key里填的包名一样。

添加依赖

在app目录下的build.gradle里的dependencies{}花括号里面添加如下依赖:

    implementation 'com.squareup.okhttp3:okhttp:3.9.0'
	implementation 'com.google.code.gson:gson:2.8.6'

然后,点击右上角的弹出的“Sync Now”来同步保存。

下载并引用和风天气的Android SDK

和风天气Android SDK
在这里插入图片描述
点击下载,然后把下载好的Android SDK拷贝到我们的Weather工程下的app目录里的libs文件夹中:
在这里插入图片描述
接着,在这个文件上右键,选择“Add As Library…”:
在这里插入图片描述
接着,点击OK:
在这里插入图片描述
若出现三角符号,就成功引用了下载好的和风天气Android SDK了。
在这里插入图片描述

获取和风天气API接口的数据

创建好布局

activity._main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <TextView
        android:text="天气"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="24sp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

    <LinearLayout
        android:layout_margin="20dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_margin="10dp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <TextView
                android:id="@+id/province_tv"
                android:text="省份:"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"/>
            <EditText
                android:id="@+id/province_edit"
                android:layout_toRightOf="@+id/province_tv"
                android:hint="请输入要查询的天气的省份"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_margin="10dp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <TextView
                android:id="@+id/city_tv"
                android:text="城市:"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"/>
            <EditText
                android:id="@+id/city_edit"
                android:layout_toRightOf="@+id/city_tv"
                android:hint="请输入该省份的城市"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </RelativeLayout>

        <Button
            android:id="@+id/search"
            android:text="查询"
            android:layout_alignParentRight="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <TextView
        android:id="@+id/city_weather"
        
  • 19
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值