Android使用ButterKnife与和风天气sdk

Android使用ButterKnife与和风天气sdk获取实况天气数据(二)

关于

我们这篇只是使用Butterknife来绑定控件(综合使用),关于和风天气sdk的详细使用参考上一篇Android和风天气sdk,这一篇只是直接pull上,(如果你用这篇来参考学习和风sdk可能就会有各种问题(毕竟新版坑较多))
Butterknife 版本10.2.0(不用8.8.1的原因是我的studio不支持了,报错提示我更新)
Android studio 3.2.0 但是我还是会讲一下8.8.1的引用(毕竟这两个在引用方法上只是数字发生变化,实际使用是不同的)
关于可能会出现的问题请参考:转载一些本人学习过程遇到的问题以及解决的博客链接
如果sync完报error提示编译版本或者java版本过低之类的,请在项目中添加如下:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
ButterKnife的项目库地址: https://github.com/JakeWharton/butterknife

网上一篇对ButterKnife的讲解:Android Butterknife使用方法总结
但是比较时间比较久了,有些东西参考就行,实践出真理!

效果

在这里插入图片描述

ButterKnife引用

首先在工程build.gradle中添加一行如下:

buildscript {
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.0'//添加这一行
      //classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1' 8.8.1也要添加
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

在项目build.gradle中添加如下:

compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }
    dependencies {
implementation 'com.jakewharton:butterknife:10.2.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
//implementation 'com.jakewharton:butterknife:8.8.1'
//annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//下面是和风库引用
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
implementation 'com.google.code.gson:gson:2.6.2'
 implementation files('libs/HeWeather_Public_Android_V3.1.jar')
.........
}

好啦,部署完成开始使用。

第一步,修改activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:background="#FFF"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_tianqi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        />
    <TextView
        android:id="@+id/tv_kongqi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        />
    <TextView
        android:id="@+id/tv_airqlty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
       />

</LinearLayout>

修改MainActicity.java(今天的重头戏)

package com.example.weathertest;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import butterknife.BindViews;
import butterknife.ButterKnife;
import interfaces.heweather.com.interfacesmodule.bean.Lang;
import interfaces.heweather.com.interfacesmodule.bean.Unit;
import interfaces.heweather.com.interfacesmodule.bean.air.now.AirNow;
import interfaces.heweather.com.interfacesmodule.bean.weather.now.Now;
import interfaces.heweather.com.interfacesmodule.view.HeConfig;
import interfaces.heweather.com.interfacesmodule.view.HeWeather;

public class MainActivity extends AppCompatActivity {
    String TAG="MainActivity";
   // TextView tv_tianqi,tv_kongqi,tv_airqlty;
    @BindViews({R.id.tv_tianqi,R.id.tv_kongqi,R.id.tv_airqlty})
    List<TextView>textViewList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        HeConfig.init("HE1909121339201612","083fbdf0edca4082813382b9551043e9");
        HeConfig.switchToFreeServerNode();
       //使用时要绑定
        ButterKnife.bind(this);
       // tv_tianqi =(TextView) findViewById(R.id.tv_tianqi);
      //  tv_kongqi =(TextView) findViewById(R.id.tv_kongqi);   
       // tv_airqlty =(TextView) findViewById(R.id.tv_airqlty);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getWether(); 
    }

    private void getWether() {
        /**
         * 实况天气
         * 实况天气即为当前时间点的天气状况以及温湿风压等气象指数,具体包含的数据:体感温度、
         * 实测温度、天气状况、风力、风速、风向、相对湿度、大气压强、降水量、能见度等。
         *
         * @param context  上下文
         * @param location 地址详解
         * @param lang       多语言,默认为简体中文
         * @param unit        单位选择,公制(m)或英制(i),默认为公制单位
         * @param listener  网络访问回调接口
         */
        HeWeather.getWeatherNow(MainActivity.this, "CN101190101",  Lang.CHINESE_SIMPLIFIED , Unit.METRIC , new HeWeather.OnResultWeatherNowBeanListener() {
            public static final String TAG="he_feng_now";
            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError: ", e);
                System.out.println("Weather Now Error:"+new Gson());
            }

            @Override
            public void onSuccess(Now dataObject) {
                Log.i(TAG, " Weather Now onSuccess: " + new Gson().toJson(dataObject));
                String jsonData = new Gson().toJson(dataObject);
                System.out.println("返回的数据内容:"+dataObject.getStatus());
                String tianqi = null,wendu = null, tianqicode = null;
                if (dataObject.getStatus().equals("ok")){
                    String JsonNow = new Gson().toJson(dataObject.getNow());
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(JsonNow);
                        tianqi = jsonObject.getString("cond_txt");
                        wendu = jsonObject.getString("tmp");
                        tianqicode = jsonObject.getString("cond_code");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    Toast.makeText(MainActivity.this,"有错误",Toast.LENGTH_SHORT).show();
                    return;
                }
                String wendu2 = wendu +"℃";
                textViewList.get(0).setText("当前天气:"+tianqi);
                textViewList.get(1).setText("当前温度:"+wendu2);           
            }
        });
        HeWeather.getAirNow(MainActivity.this, "CN101190101", Lang.CHINESE_SIMPLIFIED, Unit.METRIC, new HeWeather.OnResultAirNowBeansListener() {
           public static final String TAG2="he_feng_air";
            @Override
            public void onError(Throwable throwable) {
                Log.i(TAG2,"ERROR IS:",throwable);
            }

            @Override
            public void onSuccess(AirNow airNow) {
                Log.i(TAG2,"Air Now onSuccess:"+new Gson().toJson(airNow));
                String airStatus = airNow.getStatus();
                if (airStatus.equals("ok")){
                    String jsonData = new Gson().toJson(airNow.getAir_now_city());
                    String aqi = null,qlty = null;
                    JSONObject objectAir = null;
                    try {
                        objectAir = new JSONObject(jsonData);
                        aqi = objectAir.getString("aqi");
                        qlty = objectAir.getString("qlty");
                        textViewList.get(2).setText("天气状况:"+qlty+" "+"空气质量:"+"("+aqi+")");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    Toast.makeText(MainActivity.this,"有错误",Toast.LENGTH_SHORT).show();
                    return;
                }
            }

        });
    }
}

到此结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪の星空朝酱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值