安卓通过网络接口实现天气预报的代码
时间: 2023-10-31 21:05:25 浏览: 47
以下是一个使用OkHttp进行网络请求获取天气数据并解析的示例代码:
1. 添加依赖库
在app模块的build.gradle文件中添加依赖库:
```
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
```
2. 发送网络请求
在需要获取天气数据的地方,发送网络请求获取数据:
```
OkHttpClient client = new OkHttpClient();
String url = "http://www.weather.com.cn/data/cityinfo/101010100.html";
Request request = new Request.Builder()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// 请求失败
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// 请求成功,获取数据
String responseData = response.body().string();
// 解析数据并更新UI
updateWeather(responseData);
}
});
```
在示例中,发送一个GET请求获取北京市的天气数据,请求的URL是http://www.weather.com.cn/data/cityinfo/101010100.html。请求成功后,通过response.body().string()获取到返回的数据,解析数据并更新UI。
3. 解析天气数据并更新UI
解析天气数据的具体方式根据天气数据接口返回的数据格式进行调整。以下是一个示例解析函数:
```
private void updateWeather(String responseData) {
try {
JSONObject jsonObject = new JSONObject(responseData);
JSONObject weatherInfo = jsonObject.getJSONObject("weatherinfo");
String cityName = weatherInfo.getString("city");
String weather = weatherInfo.getString("weather");
String temp1 = weatherInfo.getString("temp1");
String temp2 = weatherInfo.getString("temp2");
String updateTime = weatherInfo.getString("ptime");
// 更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
mCityNameTextView.setText(cityName);
mWeatherTextView.setText(weather);
mTemperatureTextView.setText(temp1 + " - " + temp2);
mUpdateTimeTextView.setText("更新时间:" + updateTime);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
```
在示例中,首先将返回的数据转换成JSONObject对象,然后根据JSON键值获取到天气信息,最后将解析结果更新到UI上。
4. 处理异常情况
在发送网络请求和解析天气数据的过程中,可能会出现异常情况,例如网络异常、解析异常等。可以在代码中进行相应的处理,例如提示用户网络异常或者提供默认的天气信息。
完整代码示例:(需要在布局文件中添加相应的TextView控件)
```
public class MainActivity extends AppCompatActivity {
private TextView mCityNameTextView;
private TextView mWeatherTextView;
private TextView mTemperatureTextView;
private TextView mUpdateTimeTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCityNameTextView = findViewById(R.id.city_name_text_view);
mWeatherTextView = findViewById(R.id.weather_text_view);
mTemperatureTextView = findViewById(R.id.temperature_text_view);
mUpdateTimeTextView = findViewById(R.id.update_time_text_view);
OkHttpClient client = new OkHttpClient();
String url = "http://www.weather.com.cn/data/cityinfo/101010100.html";
Request request = new Request.Builder()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// 请求失败
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// 请求成功,获取数据
String responseData = response.body().string();
// 解析数据并更新UI
updateWeather(responseData);
}
});
}
private void updateWeather(String responseData) {
try {
JSONObject jsonObject = new JSONObject(responseData);
JSONObject weatherInfo = jsonObject.getJSONObject("weatherinfo");
String cityName = weatherInfo.getString("city");
String weather = weatherInfo.getString("weather");
String temp1 = weatherInfo.getString("temp1");
String temp2 = weatherInfo.getString("temp2");
String updateTime = weatherInfo.getString("ptime");
// 更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
mCityNameTextView.setText(cityName);
mWeatherTextView.setText(weather);
mTemperatureTextView.setText(temp1 + " - " + temp2);
mUpdateTimeTextView.setText("更新时间:" + updateTime);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}
```