移動應用開發實驗十 —— 網路通信
- 一、實驗目的
- 二、實驗內容
- 三、實驗程序(實驗的設計思路、關鍵源代碼等)
- 四、實驗結果(實驗最終作品截圖說明)
- 五、實驗心得
一、實驗目的
掌握資料決議(以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中,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/386638.html
標籤:其他
