Android地区选择器与天气预报接口应用
这是一个用CityPicker和聚合数据API实现的地区选择天气预报的应用,旨在供大家参考(如有转载,请表明作者)。
一、使用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进行解析,得到想要的数据
/**
* 使用的是聚合数据的天气预报接口API
* GET返回的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);
}
以上就是天气预报功能实现的全过程,如有问题欢迎纠错!!!
欢迎各位大咖指点