移动应用开发实验十 —— 网络通信
一、实验目的
掌握数据解析(以json为例)、网络请求的关键内容。
二、实验内容
1.接口示例(北京)http://www.weather.com.cn/data/sk/101010100.html
2.可选择和任意切换北京、杭州、广州、上海的天气(阴晴雨雪)、气温、湿度等。
3.解析json接口获取的数据。
4.在app中展现天气结果。
中国天气网地址:http://www.weather.com.cn
请求服务 : 查询实时天气信息
http://www.weather.com.cn/data/sk/101110101.html
其中101110101是城市的代码,如果要查询其他城市的天气,只需要修改城市的代码即可,在中国天气网中城市代码如下:
101010100=北京
101020100=上海
101210101=杭州
101280101=广州
三、实验过程(实验的设计思路、关键源代码等)
接口代码配置
public static final String Host_WEATHER="http://www.weather.com.cn";
@GET("/data/cityinfo/{cityId}.html")
Call<CityInfoWeather> getCityInfoWeather(@Path("cityId")String cityId);
@GET("/data/sk/{cityId}.html")
Call<SKWeather> getSKWeather(@Path("cityId")String cityId);
接口具体返回值详情
具体布局省略,根据个人意愿进行自定义布局
以下仅供参考
获取对应的气温,风向,湿度,天气情况等信息
public class SKWeather {
private WeatherInfo weatherinfo;
public WeatherInfo getWeatherInfo(){
return weatherinfo;
}
public void setWeatherInfo(WeatherInfo weatherinfo){
this.weatherinfo=weatherinfo;
}
public class WeatherInfo{
private String temp;
private String WD;
private String SD;
private String WS;
private String Humidity;
private String AP;
public String getTemp(){
return temp;}
public void setTemp(String temp){
this.temp=temp;}
public String getWD(){
return WD;}
public void WD(String WD){
this.WD=WD;}
public String getSD(){
return SD;}
public void