①首先注册一个API,打开和风天气网站和风天气开发服务 ~ 强大、丰富的天气数据服务,注册并登录。
②登录之后,点击“开发服务控制台”
③创建项目
④创建完项目之后可以查看key
⑤找到开发文档中的城市天气,每日天气预报
⑥要想获取南昌的3天天气预报,要改变location。
查询南昌的locationID替换
https://api.qweather.com/v7/weather/3d?location=101240101&key=YOUR_KEY
⑦如果是免费订阅,将上述API Host更改为devapi.qweather.com
https://devapi.qweather.com/v7/weather/3d?location=101240101&key=YOUR_KEY
⑧MainActivity代码
注意改成你自己的KEY
package com.example.myapplication;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private TextView weatherTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weatherTextView = findViewById(R.id.weatherTextView);
// 执行异步任务,获取天气数据
new FetchWeatherTask().execute();
}
private class FetchWeatherTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String weatherJsonString = null;
try {
// 创建URL对象
URL url = new URL("https://devapi.qweather.com/v7/weather/3d?location=101240101&key=你自己的KEY");
// 打开连接
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// 读取输入流
InputStream inputStream = urlConnection.getInputStream();
StringBuilder builder = new StringBuilder();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
if (builder.length() == 0) {
return null;
}
weatherJsonString = builder.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
return weatherJsonString;
}
@Override
protected void onPostExecute(String weatherJsonString) {
if (weatherJsonString != null) {
try {
JSONObject jsonObject = new JSONObject(weatherJsonString);
JSONArray dailyArray = jsonObject.getJSONArray("daily");
StringBuilder weatherInfoBuilder = new StringBuilder();
for (int i = 0; i < dailyArray.length(); i++) {
JSONObject dayForecast = dailyArray.getJSONObject(i);
String fxDate = dayForecast.getString("fxDate");
String tempMax = dayForecast.getString("tempMax");
String tempMin = dayForecast.getString("tempMin");
String textDay = dayForecast.getString("textDay");
String textNight = dayForecast.getString("textNight");
weatherInfoBuilder.append("日期: ").append(fxDate).append("\n")
.append("最高温度: ").append(tempMax).append("°C").append("\n")
.append("最低温度: ").append(tempMin).append("°C").append("\n")
.append("白天天气: ").append(textDay).append("\n")
.append("夜间天气: ").append(textNight).append("\n\n");
}
weatherTextView.setText(weatherInfoBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
⑨布局代码
<?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">
<TextView
android:id="@+id/weatherTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
注意:在AndroidMainfest.xml中加入权限声明
⑩最终效果