andriod实现天气预报app代码
时间: 2023-10-30 07:09:02 浏览: 38
以下是一个简单的 Android 天气预报应用程序的示例代码:
1. 首先创建一个新的 Android 项目。
2. 在布局文件中添加以下内容:
```
<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:id="@+id/tv_city_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:text="City Name" />
<TextView
android:id="@+id/tv_weather_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textAlignment="center"
android:text="Weather Description" />
<ImageView
android:id="@+id/iv_weather_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="Weather Icon"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/tv_temperature"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textAlignment="center"
android:text="Temperature" />
<TextView
android:id="@+id/tv_humidity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textAlignment="center"
android:text="Humidity" />
<TextView
android:id="@+id/tv_wind_speed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textAlignment="center"
android:text="Wind Speed" />
</LinearLayout>
```
3. 在 MainActivity.java 文件中添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private TextView tvCityName;
private TextView tvWeatherDescription;
private ImageView ivWeatherIcon;
private TextView tvTemperature;
private TextView tvHumidity;
private TextView tvWindSpeed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvCityName = findViewById(R.id.tv_city_name);
tvWeatherDescription = findViewById(R.id.tv_weather_description);
ivWeatherIcon = findViewById(R.id.iv_weather_icon);
tvTemperature = findViewById(R.id.tv_temperature);
tvHumidity = findViewById(R.id.tv_humidity);
tvWindSpeed = findViewById(R.id.tv_wind_speed);
// Replace 'YOUR_API_KEY' with your OpenWeatherMap API key
String apiKey = "YOUR_API_KEY";
String cityName = "London"; // Replace with the city name you want to get weather data for
String apiUrl = String.format("https://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s", cityName, apiKey);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, apiUrl, null,
response -> {
try {
JSONObject mainObject = response.getJSONObject("main");
JSONObject windObject = response.getJSONObject("wind");
JSONArray weatherArray = response.getJSONArray("weather");
JSONObject weatherObject = weatherArray.getJSONObject(0);
tvCityName.setText(cityName);
tvWeatherDescription.setText(weatherObject.getString("description"));
tvTemperature.setText(String.format("%d °C", Math.round(mainObject.getDouble("temp") - 273.15)));
tvHumidity.setText(String.format("Humidity: %d %%", mainObject.getInt("humidity")));
tvWindSpeed.setText(String.format("Wind Speed: %d m/s", windObject.getInt("speed")));
String iconCode = weatherObject.getString("icon");
String iconUrl = String.format("https://openweathermap.org/img/w/%s.png", iconCode);
Glide.with(this).load(iconUrl).into(ivWeatherIcon);
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
error.printStackTrace();
});
Volley.newRequestQueue(this).add(request);
}
}
```
请注意,此示例使用了 Volley 和 Glide 库。您需要在 build.gradle 文件中添加以下依赖项:
```
dependencies {
implementation 'com.android.volley:volley:1.2.1'
implementation 'com.github.bumptech.glide:glide:4.12.0'
}
```
此示例仅获取伦敦的天气数据。您可以使用其他城市的名称替换 cityName 变量,以获取该城市的天气数据。您还需要将 "YOUR_API_KEY" 替换为您的 OpenWeatherMap API 密钥。