移动应用开发实验十 —— 网络通信

一、实验目的

掌握数据解析(以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 SD(String SD){this.SD=SD;}
        public String getWS(){return WS;}
        public void setWS(String WS){this.WS=WS;}
        public String getHumidity(){return Humidity;}
        public void setHumidity(String Humidity){this.Humidity=Humidity;
        }
        public String getAP(){return AP;}
        public void setAP(String Baro){this.AP=AP;}
    }
}

获取各个控件,调用上述方法,将获取到的数据写入

public class WeatherActivity extends AppCompatActivity {
    private TextView cityNameTv;
    private TextView tempTv;
    private TextView weatherTv;
    private TextView windDirectionTv;
    private TextView windSpeedTv;
    private TextView humidityTv;
    private TextView baroTv;
    private String cityId;
    ApiService apiService;
    ApiService apiService_ip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        cityNameTv=findViewById(R.id.cityName);
        tempTv=findViewById(R.id.temp);
        weatherTv=findViewById(R.id.weather);
        windDirectionTv=findViewById(R.id.winddirect);
        windSpeedTv=findViewById(R.id.windspeed);
        humidityTv=findViewById(R.id.humidity);
        baroTv=findViewById(R.id.baro);

        cityNameTv.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent=new Intent(WeatherActivity.this, CityListActivity.class);
                startActivityForResult(intent, CityListActivity.CITY_ID_RESULT_CODE);
            }
        });
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.weather.com.cn").addConverterFactory(GsonConverterFactory.create()).build();
        apiService= retrofit.create(ApiService.class);
        refresh(CityHelper.getDefaultCityId());
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==CityListActivity.CITY_ID_RESULT_CODE){
            if(data != null){
                String cityId=data.getStringExtra(CityListActivity.CITY_ID_KEY);
                refresh(cityId);
            }
        }
    }
    private void refresh(String cityId){
        if(TextUtils.isEmpty(cityId)||cityId.equals(this.cityId)){
            return;
        }
        Call<CityInfoWeather> cityInfoCall= apiService.getCityInfoWeather(cityId);
        cityInfoCall.enqueue(new Callback<CityInfoWeather>(){
            @Override
            public void onResponse(Call<CityInfoWeather> call, final Response<CityInfoWeather> cityInfoResponse) {
                Call<SKWeather> skCall=apiService.getSKWeather(cityId);
                skCall.enqueue(new Callback<SKWeather>() {
                    @Override
                    public void onResponse(Call<SKWeather> call, Response<SKWeather> skResponse) {
                        CityInfoWeather cityInfoWeather= cityInfoResponse.body();
                        SKWeather skWeather=skResponse.body();
                        WeatherActivity.this.cityId=cityId;
                        cityNameTv.setText(getString(R.string.city_name_str,CityHelper.getInstance().getCityInfoMap().get(cityId).getName()));
                        tempTv.setText(getString(R.string.temp_str,skWeather.getWeatherInfo().getTemp()));
                        weatherTv.setText(cityInfoWeather.getWeatherinfo().getWeather());
                        windDirectionTv.setText(getString(R.string.wd_str, skWeather.getWeatherInfo().getWD()));
                        windSpeedTv.setText(getString(R.string.ws_str, skWeather.getWeatherInfo().getWS()));
                        humidityTv.setText(getString(R.string.humidity_str, skWeather.getWeatherInfo().getSD()));
                        baroTv.setText(getString(R.string.baro_str, skWeather.getWeatherInfo().getAP()));
                    }
                    @Override
                    public void onFailure(Call<SKWeather> call, Throwable t) {
                        Toast.makeText(WeatherActivity.this,"请求SK接口失败", Toast.LENGTH_LONG).show();
                    }
                });
            }
            @Override
            public void onFailure(Call<CityInfoWeather> call, Throwable t) {
                Toast.makeText(WeatherActivity.this,"请求CityInfo接口失败", Toast.LENGTH_LONG).show();
            }

        });
    }
}

四、实验结果(实验最终作品截图说明)

在这里插入图片描述

~~效果图~~

五、实验心得

通过本次实验,了解了如何通过配置接口访问达到网络通信的效果,以及如何解析JSON数据并将数据展示在app中。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>