Android地區選擇器與天氣預報接口應用
這是一個用CityPicker和聚合資料API實作的地區選擇天氣預報的應用,旨在供大家參考(如有轉載,請表明作者)。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL1UleOlXWq5UeRpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL1YzNwUDNwETMwMDMxkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
一、使用CityPicker地區選擇器,進行地區選擇
1、标題 title(String)
2、大小 textSize(int)
3、文字顔色|背景顔色
titleBackgroundColor(Color)
titleTextColor(Color)
backgroundPop(Color)
confirTextColor(Color)
cancelTextColor(Color)
4、三級地區
province(“xx省”)
city(“xx市”)
district(“xx區”)
5、省市縣 是否循環顯示
provinceCyclic(boolean)
cityCyclic(boolean)
districtCyclic(boolean)
6、item的個數與間距
visibleItemsCount(int)
itemPadding(int)
詳細代碼如下:
mCP = new CityPicker.Builder(view.getContext())
.textSize(20)
//位址選擇
.title("位址選擇")
.backgroundPop(0xa0000000)
//文字的顔色
.titleBackgroundColor("#0CB6CA")
.titleTextColor("#000000")
.backgroundPop(0xa0000000)
.confirTextColor("#000000")
.cancelTextColor("#000000")
.province("xx省")
.city("xx市")
.district("xx區")
//滑輪文字的顔色
.textColor(Color.parseColor("#000000"))
.provinceCyclic(true)
.cityCyclic(false)
.districtCyclic(false)
.visibleItemsCount(7)
.itemPadding(10)
.onlyShowProvinceAndCity(false)
.build();
二、使用聚合資料天氣預報API
1、導入okHttp庫,用于網絡通路
implementation ‘com.squareup.okhttp3:okhttp:4.0.1’
可以上GitHub官網檢視最新文檔
2、導入Gson庫,用于解析接口傳回的json資料
implementation ‘com.google.code.gson:gson:2.7’
3、在聚合資料上申請天氣預報API
4、添加網絡權限
5、利用okHttp通路API,擷取傳回的json資料,再用Gson進行解析,得到想要的資料
private void getWeather(){
new Thread(new Runnable() {
@Override
public void run() {
Request request=new Request.Builder()
.url("http://apis.juhe.cn/simpleWeather/query?city="+person_city+"&key=dc9c28debe2439835ebe273a329ed8d9")
.build();
final okhttp3.OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
OkHttpClient okHttpClient = httpBuilder
//設定逾時
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
Log.d("MainActivity","http://apis.juhe.cn/simpleWeather/query?city="+person_city+"&key=dc9c28debe2439835ebe273a329ed8d9");
try {
Response response=okHttpClient.newCall(request).execute();
String responseData=response.body().string();
showResponse(responseData);
}catch (IOException e){
e.printStackTrace();
}
Message message=new Message();
message.what=UPDATE_WEATHER;
handler.sendMessage(message); //将Message對象發送出去
}
}).start();
}
//解析傳回的資料
private void showResponse(String responseData){
Gson gson=new Gson();
Return_Weather returnWeather=gson.fromJson(responseData,Return_Weather.class);
String reason=returnWeather.reason;
final WeatherHour result=returnWeather.result;
final WeatherDay future=returnWeather.future;
String error_code=returnWeather.error_code;
Log.d("MainActivity","reason是"+reason+" result是"+result+" future是"+future+" error_code是"+error_code);
String city=result.city;
final WeatherTime realtime=result.realtime;
Log.d("MainActivity","city是"+city+" realtime是"+realtime);
temperature=realtime.temperature;
humidity=realtime.humidity;
info=realtime.info;
wid=realtime.wid;
direct=realtime.direct;
power=realtime.power;
aqi=realtime.aqi;
Log.d("MainActivity","溫度是:"+temperature+" 濕度是:"+humidity+" 天氣是:"+info
+" 風向是:"+direct+" 風級是:"+power+" 空氣品質是:"+aqi);
}
以上就是天氣預報功能實作的全過程,如有問題歡迎糾錯!!!
歡迎各位大咖指點