利用android实现天气预报代码
时间: 2023-08-01 07:14:18 浏览: 138
以下是一个简单的 Android 天气预报应用的代码示例:
1. 创建一个新的 Android 项目,并在 MainActivity.java 文件中添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private TextView mCityTextView;
private TextView mTemperatureTextView;
private TextView mDescriptionTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCityTextView = findViewById(R.id.city_textview);
mTemperatureTextView = findViewById(R.id.temperature_textview);
mDescriptionTextView = findViewById(R.id.description_textview);
getWeatherData();
}
private void getWeatherData() {
String url = "https://api.openweathermap.org/data/2.5/weather?q=London,uk&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 {
JSONObject main = response.getJSONObject("main");
JSONArray weather = response.getJSONArray("weather");
JSONObject weatherObject = weather.getJSONObject(0);
String city = response.getString("name");
String temperature = main.getString("temp");
String description = weatherObject.getString("description");
mCityTextView.setText(city);
mTemperatureTextView.setText(temperature + "°C");
mDescriptionTextView.setText(description);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
}
}
```
2. 在 activity_main.xml 文件中添加以下代码:
```xml
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/city_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<TextView
android:id="@+id/temperature_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="70sp"
android:textStyle="bold"
android:layout_below="@+id/city_textview"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/description_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="italic"
android:layout_below="@+id/temperature_textview"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
</RelativeLayout>
```
3. 在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.INTERNET" />
```
4. 在 build.gradle 文件中添加以下依赖项:
```gradle
dependencies {
implementation 'com.android.volley:volley:1.1.1'
}
```
5. 将 YOUR_API_KEY 替换为您的 OpenWeatherMap API 密钥。
这个示例使用了 Volley 库来处理网络请求并从 OpenWeatherMap API 中获取天气数据。它显示了城市名、温度和天气描述。您可以根据需要对代码进行修改和扩展。