不多BB,直接上代码!
①项目准备
compile 'io.reactivex.rxjava2:rxjava:2.0.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.0'
compile 'org.reactivestreams:reactive-streams:1.0.0'
compile'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okio:okio:1.10.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
②项目结构
一个主体类MainActivity,接口WeathearService,Bean类Weather
(上面拼单词的时候...weather拼成weathear...英语水平不够,见谅。。。)
③接口类
public interface WeathearService {
@GET("x3/weather") Flowable getWeather(
@Query("city")String city, @Query("key")String keyName);
}
//get请求方式, Flowable 记住这里传入的是你的Bean类(Weather)
//x3/weather 是基本url后面拼接的字符串
// @Query("city")String city 代表的意思是URL后面携带的参数city=传入的城市,key则是你申请到的一个密钥值
④主体类
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.heweather.com/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())// 添加RxJava2的适配器支持 必须要加。
.addConverterFactory(GsonConverterFactory.create())
.build();mWeathearService = retrofit.create(WeathearService.class);
//初始化Retrofit
// addCallAdapyerFactory 是创建了第三方库封装好的adapter
private void initUI() {
mTextView = (TextView) findViewById(R.id.weather);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mWeathearService.getWeather("广州", "---你申请到的key值--")//网络请求在子线程
.subscribeOn(Schedulers.io())订阅者要在主线程执行
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber() {
@Override
public void onSubscribe(Subscription s) {
//请求次数,设置最大值即可
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(Weather weather) {
String s = weather.result.get(0).aqi.city.toString();
Log.i("sea", "weather:" + s);
mTextView.setText(s); }
@Override
public void onError(Throwable t) {
//当网络请求出错会执行这里
Log.i("sea", t.getMessage());
}
@Override
public void onComplete() {
//执行这里全部操作结束
Log.i("sea", "获取结束");
}
});
}
});
}
⑤bean类(比较长,重点理解在实体类和接口类)
public class Weather {
@SerializedName("HeWeather data service 3.0")//json数据头的替换
public ArrayList result;
/***信息的全部内容*/
public class Result{
public Aqi aqi;
public CityBasic basic;
public Now now;
public ArrayList daily_forecast;
public ArrayList hourly_forecast;
public String status;
public Suggestion suggestion;
}
public class CityBasic{
@Override
public String toString() {
return "CityBasic{" +
"city='" + city + '\'' +
", cnty='" + cnty + '\'' +
", id='" + id + '\'' +
", lat='" + lat + '\'' +
", lon='" + lon + '\'' +
", update=" + update +
'}';
}
public String city;//城市名称
public String cnty;//城市国家
public String id;//城市id
public String lat;
public String lon;
public Update update;
}
public class Update{
public String loc;//当地时间
public String utc;//更新时间 }
//======================================================
public class Aqi{
public AqiCity city;
}
public class AqiCity{
@Override
public String toString() {
return "AqiCity{" +
"aqi='" + aqi + '\'' +
", co='" + co + '\'' +
", no2='" + no2 + '\'' +
", o3='" + o3 + '\'' +
", pm10='" + pm10 + '\'' +
", pm25='" + pm25 + '\'' +
", qlty='" + qlty + '\'' +
", so2='" + so2 + '\'' +
'}';
}
public String aqi;
public String co;
public String no2;
public String o3;
public String pm10;
public String pm25;
public String qlty;
public String so2;
}
//==============================================
public class Now{
public Cond cond;//天气状况
public String fl;//体感温度
public String hum;//相对湿度(%)
public String pcpm;//降水量(mm)
public String pres;//气压
public String tmp;//温度
public String vis;//能见度(km)
public Wind wind;//风力风向
}
public class Cond{
public String code;
public String txt;
}
public class Wind{
public String deg;//风向(360度)
public String dir;//风向
public String sc;//风力
public String spd;//风速(kmph)
}
//================================================
public class Daily{
public Astro astro;//未来日出日落
public DailyCond cond;//未来7天天气状况
public String date;//时间
public String hum;//相对湿度(%)
public String pcpn;//降水量(mm)
public String pop;//降水概率
public String pres;//气压
public String vis;//能见度
public Temp tmp;//温度
}
public class Astro{
public String sr;//日出时间
public String ss;//日落时间
}
public class DailyCond{
public String code_d;//白天天气状况图片
public String code_n;//晚上天气状况图片
public String txt_d;//白天天气描述
public String txt_n;//晚上天气描述
}
public class Temp{
public String max;//最高温度
public String min;//最低温度
}
//=============================================
public class Hourly{
public String date;//2015-07-02 01:00", //时间
public String hum;//相对湿度(%)
public String pop;//降水概率
public String pres; //气压
public String tmp;//温度
public HourWind wind;
}
public class HourWind{
public String deg;//风向(360度)
public String dir; //风向
public String sc; //风力
public String spd;//风速(kmph)
}
//=====================================
public class Suggestion{
public Uv uv;
public Comf comf;
public Drsg drsg;
public Flu flu;
public Sport sport;
public Trav trav;
}
public class Uv{
public String brf;//紫外线强度
public String txt;//描述
}
public class Comf{
public String brf;
public String txt;
}
public class Drsg{
public String brf;
public String txt;
}
public class Flu{
public String brf;
public String txt;
}
public class Sport{
public String brf;
public String txt;
}
public class Trav{
public String brf;
public String txt;
}
}
⑥运行效果
ps:如有不恰当的地方,欢迎指出O(∩_∩)O。