retrofit+rxjava的基本用法

首先添加依赖

    //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'

然后网络权限不要忘记添加了

    <uses-permission android:name="android.permission.INTERNET"/>

然后是网络管理类

/**
 * Created by pw on 2019/6/10 14:03
 * E-Mail Address: pw163.com
 */
public interface InterService {
    @GET("weather_mini")
    Call<WeatherRsp> getMessage(@Query("city") String city);

    @GET("weather_mini")
    Observable<WeatherRsp> 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<WeatherRsp> c = weatherService.getMessage("北京");
        c.enqueue(new Callback<WeatherRsp>() {
            @Override
            public void onResponse(Call<WeatherRsp> call, Response<WeatherRsp> response) {
                WeatherRsp body = response.body();
                tv_datas.setText(body.data.ganmao.trim());
            }

            @Override
            public void onFailure(Call<WeatherRsp> 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<WeatherRsp>() {
                    @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":"<![CDATA[4-5级]]>","type":"晴"},"city":"北京","forecast":[{"date":"10日星期一","high":"高温 33℃","fengli":"<![CDATA[3-4级]]>","low":"低温 18℃","fengxiang":"东南风","type":"晴"},{"date":"11日星期二","high":"高温 32℃","fengli":"<![CDATA[3-4级]]>","low":"低温 20℃","fengxiang":"东南风","type":"晴"},{"date":"12日星期三","high":"高温 28℃","fengli":"<![CDATA[<3级]]>","low":"低温 20℃","fengxiang":"南风","type":"雷阵雨"},{"date":"13日星期四","high":"高温 32℃","fengli":"<![CDATA[<3级]]>","low":"低温 20℃","fengxiang":"东南风","type":"多云"},{"date":"14日星期五","high":"高温 35℃","fengli":"<![CDATA[<3级]]>","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":"<![CDATA[4-5级]]>","type":"晴"}
         * city : 北京
         * forecast : [{"date":"10日星期一","high":"高温 33℃","fengli":"<![CDATA[3-4级]]>","low":"低温 18℃","fengxiang":"东南风","type":"晴"},{"date":"11日星期二","high":"高温 32℃","fengli":"<![CDATA[3-4级]]>","low":"低温 20℃","fengxiang":"东南风","type":"晴"},{"date":"12日星期三","high":"高温 28℃","fengli":"<![CDATA[<3级]]>","low":"低温 20℃","fengxiang":"南风","type":"雷阵雨"},{"date":"13日星期四","high":"高温 32℃","fengli":"<![CDATA[<3级]]>","low":"低温 20℃","fengxiang":"东南风","type":"多云"},{"date":"14日星期五","high":"高温 35℃","fengli":"<![CDATA[<3级]]>","low":"低温 21℃","fengxiang":"西南风","type":"晴"}]
         * ganmao : 各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。
         * wendu : 32
         */

        public YesterdayBean yesterday;
        public String city;
        public String ganmao;
        public String wendu;
        public List<ForecastBean> forecast;

        public static class YesterdayBean {
            /**
             * date : 9日星期日
             * high : 高温 31℃
             * fx : 东北风
             * low : 低温 18℃
             * fl : <![CDATA[4-5级]]>
             * 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 : <![CDATA[3-4级]]>
             * low : 低温 18℃
             * fengxiang : 东南风
             * type : 晴
             */

            public String date;
            public String high;
            public String fengli;
            public String low;
            public String fengxiang;
            public String type;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值