android studio天气预报代码和布局文件
时间: 2023-07-27 13:20:12 浏览: 132
以下是一个简单的 Android Studio 天气预报应用的代码和布局文件,仅供参考。
布局文件(activity_main.xml):
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/city_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="城市名称"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="@+id/icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/city_name"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
<TextView
android:id="@+id/temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="温度"
android:layout_below="@+id/icon"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
<TextView
android:id="@+id/weather_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="天气描述"
android:layout_below="@+id/temperature"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/get_weather_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取天气"
android:layout_below="@+id/weather_desc"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
</RelativeLayout>
```
Java 代码(MainActivity.java):
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
// 请在 https://openweathermap.org/ 上注册账号并获取 API Key
private static final String API_KEY = "YOUR_API_KEY_HERE";
private static final String API_URL = "https://api.openweathermap.org/data/2.5/weather?q=%s&units=metric&appid=%s";
private TextView cityNameTextView, temperatureTextView, weatherDescTextView;
private ImageView iconImageView;
private Button getWeatherButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityNameTextView = findViewById(R.id.city_name);
temperatureTextView = findViewById(R.id.temperature);
weatherDescTextView = findViewById(R.id.weather_desc);
iconImageView = findViewById(R.id.icon);
getWeatherButton = findViewById(R.id.get_weather_btn);
getWeatherButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new WeatherTask().execute("Beijing");
}
});
}
private class WeatherTask extends AsyncTask<String, Void, JSONObject> {
@Override
protected JSONObject doInBackground(String... strings) {
try {
URL url = new URL(String.format(API_URL, strings[0], API_KEY));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
connection.disconnect();
return new JSONObject(builder.toString());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
if (jsonObject != null) {
try {
cityNameTextView.setText(jsonObject.getString("name"));
temperatureTextView.setText(jsonObject.getJSONObject("main").getString("temp") + " ℃");
weatherDescTextView.setText(jsonObject.getJSONArray("weather").getJSONObject(0).getString("description"));
int iconId = getResources().getIdentifier("icon_" + jsonObject.getJSONArray("weather").getJSONObject(0).getString("icon"), "drawable", getPackageName());
iconImageView.setImageResource(iconId);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
```
这个应用利用了 OpenWeatherMap 的 API 来获取天气数据。请注意,在代码中需要替换 `API_KEY` 为你自己的 API Key。
阅读全文