android天气预报代码
时间: 2023-08-01 08:14:18 浏览: 146
以下是一个简单的Android天气预报应用程序示例,使用了OpenWeatherMap API获取天气数据:
1. 创建一个新的Android项目,并在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.INTERNET" />
```
2. 在build.gradle文件中添加以下依赖项:
```groovy
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'com.squareup.okio:okio:2.9.0'
implementation 'com.google.code.gson:gson:2.8.6'
```
3. 在activity_main.xml文件中添加以下视图:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical">
<EditText
android:id="@+id/edit_text_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter city name" />
<Button
android:id="@+id/btn_get_weather"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Weather" />
<TextView
android:id="@+id/text_view_weather"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
```
4. 在MainActivity.java文件中添加以下代码:
```java
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private EditText editTextCity;
private Button btnGetWeather;
private TextView textViewWeather;
private OkHttpClient client;
private Gson gson;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextCity = findViewById(R.id.edit_text_city);
btnGetWeather = findViewById(R.id.btn_get_weather);
textViewWeather = findViewById(R.id.text_view_weather);
client = new OkHttpClient();
gson = new Gson();
handler = new Handler(Looper.getMainLooper());
btnGetWeather.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String city = editTextCity.getText().toString();
if (city.isEmpty()) {
return;
}
getWeather(city);
}
});
}
private void getWeather(final String city) {
Request request = new Request.Builder()
.url("https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid={your_api_key}")
.build();
client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
@Override
public void onFailure(Request request, IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
textViewWeather.setText("Failed to fetch weather data");
}
});
}
@Override
public void onResponse(Response response) throws IOException {
if (!response.isSuccessful()) {
onFailure(null, null);
return;
}
final String json = response.body().string();
final WeatherData weatherData = gson.fromJson(json, WeatherData.class);
handler.post(new Runnable() {
@Override
public void run() {
textViewWeather.setText(String.format("Temperature in %s: %.1f°C", city, weatherData.main.temp - 273.15));
}
});
}
});
}
private static class WeatherData {
MainData main;
private static class MainData {
double temp;
}
}
}
```
其中,{your_api_key}应替换为您的OpenWeatherMap API密钥。
阅读全文