Android 简易天气制作(和风天气)
效果图:
准备工作:
1.和风天气官网——>戳我
2.点击天气API 登陆控制台,没有的话注册一个。
3.应用管理——>新建应用,填写应用名称,key名称,包名后得到这个
4.和风天气sdk->下载sdk
5.下载后复制到libs 右键 Add as library
6.AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
7.在build.gradle(Module:app) 中添加:
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
implementation 'com.google.code.gson:gson:2.6.2'
代码块
values->New->Values resource file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ctype">
<item>北京</item>
<item>上海</item>
<item>南阳</item>
</string-array>
</resources>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="203dp"
android:layout_height="242dp"
android:layout_marginTop="268dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tv_tmp"
android:layout_width="206dp"
android:layout_height="42dp"
android:gravity="center" />
<TextView
android:id="@+id/tv_weather"
android:layout_width="206dp"
android:layout_height="42dp"
android:gravity="center" />
</LinearLayout>
<Spinner
android:id="@+id/spinner1"
android:layout_width="112dp"
android:layout_height="36dp"
android:entries="@array/ctype"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="78dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
以这个为模板写MainActivity.java中的getWeather方法
MainActivity.java:
package com.test.fiveweather;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import interfaces.heweather.com.interfacesmodule.bean.Code;
import interfaces.heweather.com.interfacesmodule.bean.Lang;
import interfaces.heweather.com.interfacesmodule.bean.Unit;
import interfaces.heweather.com.interfacesmodule.bean.weather.now.Now;
import interfaces.heweather.com.interfacesmodule.bean.weather.now.NowBase;
import interfaces.heweather.com.interfacesmodule.view.HeConfig;
import interfaces.heweather.com.interfacesmodule.view.HeWeather;
public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity";
private Spinner spinner;
private TextView tv_weather;
private TextView tv_tmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_weather=(TextView) findViewById(R.id.tv_weather);
tv_tmp = (TextView)findViewById(R.id.tv_tmp);
HeConfig.init("准备工作3中得到的Username值","准备工作3中的key值");
HeConfig.switchToFreeServerNode();
spinner = (Spinner)findViewById(R.id.spinner1);
//设置监听
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String date = (String) spinner.getItemAtPosition(position);
if (position==0){
//CN+北京城市代码
getWeather("CN101010100");
}else if(position==1){
getWeather("CN101020100");
}else {
getWeather("CN101180701");
}
Toast.makeText(MainActivity.this,date,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void getWeather(String a){
//a表示城市代码
HeWeather.getWeatherNow(MainActivity.this, a, Lang.CHINESE_SIMPLIFIED, Unit.METRIC, new HeWeather.OnResultWeatherNowBeanListener() {
@Override
public void onError(Throwable throwable) {
Log.i(TAG,"Weather Now onError:",throwable);
}
@Override
public void onSuccess(Now now) {
String jsonData = new Gson().toJson(now);
Log.i(TAG, "Weather Now onSuccess:"+jsonData);
if(Code.OK.getCode().equalsIgnoreCase(now.getStatus())){
NowBase nowBase = now.getNow();
tv_weather.setText("天气:"+nowBase.getCond_txt());
tv_tmp.setText("温度:"+nowBase.getTmp()+"℃");
}else{
String status = now.getStatus();
Code code = Code.toEnum(status);
Log.i(TAG," "+code);
}
}
});
}
}
Ps:中国城市代码网上可以搜到。