首先添加依赖
//only Retrofit(只用Retrofit联网)
api 'com.squareup.retrofit2:retrofit:2.1.0'
api 'com.squareup.retrofit2:converter-gson:2.1.0'
//Rxjava and Retrofit(Retrofit+Rx需要添加的依赖)
api 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
api 'io.reactivex:rxandroid:1.2.1'
api 'io.reactivex:rxjava:1.2.1'
然后网络权限不要忘记添加了
然后是网络管理类
/**
* Created by pw on 2019/6/10 14:03
* E-Mail Address: pw163.com
*/
public interface InterService {
@GET("weather_mini")
Call getMessage(@Query("city") String city);
@GET("weather_mini")
Observable getDatas(@Query("city") String city);
}
然后是单纯的只用retrofit(单纯的只用retrofit回调的是Call)
/**
* 单纯使用Retrofit的联网请求
*/
private void doRequestByRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://wthrcdn.etouch.cn/")//基础URL 建议以 / 结尾
.addConverterFactory(GsonConverterFactory.create())//设置 Json 转换器
.build();
InterService interService= retrofit.create(InterService.class);
Call c = weatherService.getMessage("北京");
c.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
WeatherRsp body = response.body();
tv_datas.setText(body.data.ganmao.trim());
}
@Override
public void onFailure(Call call, Throwable t) {
Log.e("asd", "response == " + t);
}
});
}
然后是rx+rt混合使用
private void requestData() {
Retrofit build = new Retrofit.Builder()
.baseUrl("http://wthrcdn.etouch.cn/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
InterService service = build.create(InterService.class);
service.getDatas("北京").subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(WeatherRsp weatherRsp) {
tv_datas.setText(weatherRsp.data.ganmao);
}
});
}
然后是bean类
/**
* Created by pw on 2019/6/10 14:06
* E-Mail Address: pw163.com
*/
public class WeatherRsp {
/**
* data : {"yesterday":{"date":"9日星期日","high":"高温 31℃","fx":"东北风","low":"低温 18℃","fl":"","type":"晴"},"city":"北京","forecast":[{"date":"10日星期一","high":"高温 33℃","fengli":"","low":"低温 18℃","fengxiang":"东南风","type":"晴"},{"date":"11日星期二","high":"高温 32℃","fengli":"","low":"低温 20℃","fengxiang":"东南风","type":"晴"},{"date":"12日星期三","high":"高温 28℃","fengli":"","low":"低温 20℃","fengxiang":"南风","type":"雷阵雨"},{"date":"13日星期四","high":"高温 32℃","fengli":"","low":"低温 20℃","fengxiang":"东南风","type":"多云"},{"date":"14日星期五","high":"高温 35℃","fengli":"","low":"低温 21℃","fengxiang":"西南风","type":"晴"}],"ganmao":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。","wendu":"32"}
* status : 1000
* desc : OK
*/
public DataBean data;
public int status;
public String desc;
public static class DataBean {
/**
* yesterday : {"date":"9日星期日","high":"高温 31℃","fx":"东北风","low":"低温 18℃","fl":"","type":"晴"}
* city : 北京
* forecast : [{"date":"10日星期一","high":"高温 33℃","fengli":"","low":"低温 18℃","fengxiang":"东南风","type":"晴"},{"date":"11日星期二","high":"高温 32℃","fengli":"","low":"低温 20℃","fengxiang":"东南风","type":"晴"},{"date":"12日星期三","high":"高温 28℃","fengli":"","low":"低温 20℃","fengxiang":"南风","type":"雷阵雨"},{"date":"13日星期四","high":"高温 32℃","fengli":"","low":"低温 20℃","fengxiang":"东南风","type":"多云"},{"date":"14日星期五","high":"高温 35℃","fengli":"","low":"低温 21℃","fengxiang":"西南风","type":"晴"}]
* ganmao : 各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。
* wendu : 32
*/
public YesterdayBean yesterday;
public String city;
public String ganmao;
public String wendu;
public List forecast;
public static class YesterdayBean {
/**
* date : 9日星期日
* high : 高温 31℃
* fx : 东北风
* low : 低温 18℃
* fl :
* type : 晴
*/
public String date;
public String high;
public String fx;
public String low;
public String fl;
public String type;
}
public static class ForecastBean {
/**
* date : 10日星期一
* high : 高温 33℃
* fengli :
* low : 低温 18℃
* fengxiang : 东南风
* type : 晴
*/
public String date;
public String high;
public String fengli;
public String low;
public String fengxiang;
public String type;
}
}
}