retrofit网络访问使用call.enqueue修改为retrofit+rxjava+rxandroid

1 篇文章 0 订阅
1 篇文章 0 订阅

call.enqueque实现
1、jar包加载:

  compile 'io.reactivex:rxjava:x.y.z'
    compile 'io.reactivex:rxandroid:1.0.1'
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
    compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'

2、HttpService

public interface HttpService {
    /*
    请求地址:http://v.juhe.cn/toutiao/index
请求参数:type=shehui&key=9f3097f4cbe47e8abb01ca3b92e49cda
请求方式:GET
     */
    @POST("toutiao/index")
    //   Call<List<Data>> getData(@Query("type") String type, @Query("key") String key);
}

3、call.enqueue

  //1

        Retrofit retrofit = new Retrofit.Builder().
                baseUrl("http://v.juhe.cn/").
   addConverterFactory(CustomConverterFactory.create())
             .build();//CustomConverterFactory我自定义的解析器
        //2
        HttpService myService = retrofit.create(HttpService.class);
        //3
       retrofit.Call<List<Data>> call = myService.getData(type, "9f3097f4cbe47e8abb01ca3b92e49cda");

        call.enqueue(new Callback<List<Data>>() {
            @Override
            public void onResponse(Call<List<Data>> call, Response<List<Data>> response) {
                Log.d("lihui", "123onResponse");
                try {
                    List<Data> dataList = response.body();
                    Log.d("CustomConverterFactory", "  Utils.resetList(list, dataList)---" + list);
                    Utils.resetList(list, dataList);//交换数据

                    if (list != null && list.size() > 0 && mHandler != null) {
                        Message msg = mHandler.obtainMessage();
                        msg.what = 0;
                        msg.obj = list;
                        mHandler.sendMessage(msg);
                        empty.setVisibility(View.GONE);
                        cacheMap.put(type, list);
                    }
                    Log.d("lihui", "159 list---" + list);

                } catch (Exception e) {
                    Log.d("lihui", "114e---" + e.getMessage());
                    pullToRefreshGridView.onRefreshComplete();
                }
            }

            @Override
            public void onFailure(Call<List<Data>> call, Throwable t) {
                Log.d("lihui", "165t:" + t.getMessage());
                t.printStackTrace();
                Message msg = mHandler.obtainMessage();
                msg.what = 1;
                msg.obj = type;
                mHandler.sendMessage(msg);
            }
        });

好了结束了。

使用rxjava+rxandroid 结合retrofit
1、Observable

public interface HttpService {
    /*
    请求地址:http://v.juhe.cn/toutiao/index
请求参数:type=shehui&key=9f3097f4cbe47e8abb01ca3b92e49cda
请求方式:GET
Observable<List<Data>> getData
     */
    @POST("toutiao/index")
    Observable<List<Data>> getData(@Query("type") String type, @Query("key") String key);

}

2、加addCallAdapterFactory,修改成Observable模式

 //1

        Retrofit retrofit = new Retrofit.Builder().
                baseUrl("http://v.juhe.cn/").
                addConverterFactory(CustomConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();

        //2.创建接口对象,已经实现了getData方法
        HttpService myService = retrofit.create(HttpService.class);
         //4
        Observable<List<Data>> observable = myService.getData(type, "9f3097f4cbe47e8abb01ca3b92e49cda");              //获取Observable对象,这个是子线程中执行了Schedulers.io()
        observable.subscribeOn(Schedulers.io())  // 网络请求切换在io线程中调用

 /***
                .doOnNext(new Action1<List<Data>>() {//1
                    @Override
                    public void call(List<Data> dataList) {
                        //    saveUserInfo(userInfo);//保存用户信息到本地
                        Log.d("CustomConverterFactory", "doOnNext call dataList---" + dataList);
                        Log.d("CustomConverterFactory", "doOnNext call currentThread---" + Thread.currentThread().getName());
                    }
                }).doOnCompleted(new Action0() {
            @Override
            public void call() {
                Log.d("CustomConverterFactory", "doOnCompleted call currentThread---" + Thread.currentThread().getName());
            }
        })
        ***/

                .subscribe(
                        new Subscriber<List<Data>>() {//subscribe 子线程
                            @Override
                            public void onCompleted() {
                                Log.d("CustomConverterFactory", "onCompleted currentThread---" + Thread.currentThread().getName());
                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                                Log.d("CustomConverterFactory", "165t:" + e.getMessage());
                                Message msg = mHandler.obtainMessage();
                                msg.what = 1;
                                msg.obj = type;
                                mHandler.sendMessage(msg);
                                //请求失败
                                Log.d("CustomConverterFactory", "onError currentThread---" + Thread.currentThread().getName());
                            }

                            @Override
                            public void onNext(List<Data> dataList) {
                                //请求成功
                                Log.d("CustomConverterFactory", "onNext currentThread---" + Thread.currentThread().getName());
                                Utils.resetList(list, dataList);//交换数据
                                try {
                                    Log.d("CustomConverterFactory", "  Utils.resetList(list, dataList)---" + list);
                                    if (list != null && list.size() > 0 && mHandler != null) {
                                        Message msg = mHandler.obtainMessage();
                                        msg.what = 0;
                                        msg.obj = list;
                                        mHandler.sendMessage(msg);
                                        empty.setVisibility(View.GONE);
                                        cacheMap.put(type, list);
                                    }
                                    Log.d("lihui", "159 list---" + list);

                                } catch (Exception e) {
                                    Log.d("lihui", "114e---" + e.getMessage());
                                    pullToRefreshGridView.onRefreshComplete();
                                }
                            }
                        });

ps:log打印

 doOnNext call currentThread---main
  onNext currentThread---main
  doOnCompleted call currentThread---main
   onCompleted currentThread---main

这里的doOnNext 、onCompleted 代码放的位置不同,会运行在不同线程中,这里再main线程。通常,都是放在主线程的,用于输出前过滤操作!!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值