安卓期末大作业Android天气预报app源码
时间: 2023-09-11 16:06:16 浏览: 102
以下是一个简单的安卓天气预报应用程序的源代码,你可以在 Android Studio 中创建一个新的项目,将代码复制粘贴到相关文件中,然后运行应用程序。
MainActivity.java
```
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
public class MainActivity extends AppCompatActivity {
private EditText cityField;
private Button submitButton;
private TextView temperatureText;
private TextView descriptionText;
private RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化 UI 组件
cityField = findViewById(R.id.city_field);
submitButton = findViewById(R.id.submit_button);
temperatureText = findViewById(R.id.temperature_text);
descriptionText = findViewById(R.id.description_text);
// 实例化 Volley 请求队列
requestQueue = Volley.newRequestQueue(this);
// 绑定提交按钮的点击事件
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取用户输入的城市名
String city = cityField.getText().toString();
// 发送 API 请求获取天气数据
String url = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=YOUR_API_KEY&units=metric";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// 解析 JSON 数据并更新 UI
JSONArray weatherArray = response.getJSONArray("weather");
JSONObject weatherObject = weatherArray.getJSONObject(0);
String description = weatherObject.getString("description");
JSONObject mainObject = response.getJSONObject("main");
double temperature = mainObject.getDouble("temp");
temperatureText.setText(String.format("%.1f°C", temperature));
descriptionText.setText(description);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// 显示错误信息
temperatureText.setText("N/A");
descriptionText.setText("无法获取天气数据,请检查城市名是否正确。");
}
});
requestQueue.add(jsonObjectRequest);
}
});
}
}
```
此代码中,我们使用了 EditText、Button 和 TextView 组件来显示用户输入的城市名、触发获取天气数据的过程和显示天气数据。我们还使用了 Volley 库来发送 API 请求并处理响应。在 onCreate() 方法中实例化了这些组件和 Volley 请求队列,并且在提交按钮的点击事件中发送 API 请求。在响应成功的回调函数中,我们解析 JSON 数据并且更新 UI,如果发生错误则显示相应的错误信息。
activity_main.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="输入城市名"
android:textSize="20sp" />
<EditText
android:id="@+id/city_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="城市名" />
<Button
android:id="@+id/submit_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取天气数据" />
<TextView
android:id="@+id/temperature_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="N/A"
android:textAlignment="center"
android:paddingTop="32dp"
android:paddingBottom="32dp" />
<TextView
android:id="@+id/description_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="无法获取天气数据,请检查城市名是否正确。"
android:textAlignment="center" />
</LinearLayout>
```
此代码中,我们使用了 LinearLayout 和 TextView 组件来布局和显示 UI,使用了 EditText 和 Button 组件来获取用户输入和触发相应的操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)