1)配置

dependencies{
...
compile 'io.reactivex:rxjava:1.2.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.squareup.retrofit2:retorfit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
}

2)修改请求网络接口

Retrofit请求接口返回Call。若结合RxJava,则需要把Call改为Observable

public interface IpServiceForPost{
@FormUrlEncoded
@POST("getIpInfo.php")
Observable<IpModel> getIpMsg(@Field("ip") String first);
}

3)修改请求网络方法

private void postIpInformation(String ip){
String url = "http://ip.taobao.com/service/";
Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
IpServiceForPost ipService = retrofit.create(IpServcieForPost.class);
subscription=ipService.getIpMsg(ip).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<IpModel>(){
@Override
public void onCompleted(){
Log.d(TAG,"onCompleted");
}
@Override
public void onError(Throwable e){
Log.d(TAG,"onError");
}
@Override
public void onNext(IpModel ipModel){
String country=ipModel.getData().getCountry();
Toast.makeText(getApplicationContext(),country,Toast.LENGTH_SHORT).show();
}
});
}

4)请求返回数据格式封装

public class IpModel{
private int code;
private IpData data;
public void setCode(int code){
this.code = code;
}
public int getCode(){
return this.code;
}
...
}
public class HttpResult<T>{
private int code;
private T data;
public int getCode(){
return code;
}
public void setCode(int code){
this.code = code;
}
public T getData(){
return data;
}
public void setData(T data){
this.data = data;
}
}
public interface IpServiceForPost{
@FormUrlEncoded
@POST("getIpInfo.php")
Observable<HttpResult<IpData>> getIpMsg(@Field("ip") String first);
}
private void postIpInformation(String ip){
...
subscription=ipService.getIpMsg(ip).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<HttpResult<IpData>>(){
@Override
public void onCompleted(){
}
@Override
public void onError(Throwable e){
}
@Override
public void onNext(HttpResult<IpData> ipDataHttpResult){
IpData data=ipDataHttpResult.getData();
Toast.matkText(getApplicationContext(),data.getCountry(),Toast.LENGTH_SHORT).show();
}
});
}

5)取消请求

@Override
public void onStop(){
super.onStop();
if(subscription!=null && subscription.isUnsubscribed()){
subscription.unsubscribe();
}
}
private CompositeSubscription compositeSubscription = new CompositeSubscription();
...
//发送请求的地方,将返回的subscription添加到compositeSubscription中
compositeSubscription.add(subscription);
...
//取消所有请求
@Override
public void onStop(){
super.onStop();
compositeSubscription.unsubscribe();
}

《Android进阶之光》--RxJava结合Retrofit访问网络的更多相关文章

  1. 《Android进阶之光》--RxJava

    No1: RxJava使用 dependencies{ compile 'io.reactivex:rxjava:1.2.0' compile 'io.reactivex:rxandroid:1.2. ...

  2. [置顶] 曙光到来,我的新书《Android进阶之光》已出版

    独立博客版本请点击这里 由来 2016年我开始建立了自己的知识体系,所有的文章都是围绕着这个体系来写,随着这个体系的慢慢成长,开始有很多出版社联系我写书,因为比较看好电子工业出版社,就顺理成章的开始了 ...

  3. 《Android进阶之光》--注解与依赖注入框架

    No1: 标准注解: 1)@Override:覆写 2)@Deprecated:过时 3)@SuppressWarnings:取消警告 4)@SafeVarargs:申明使用了可变长度参数的方法 No ...

  4. 《Android进阶之光》--Material Design

    接上篇<Android进阶之光>--Android新特性 No1: 组件: 1)底部工作条-Bottom Sheets 2)卡片-Cards 3)提示框-Dialogs 4)菜单-Menu ...

  5. Android进阶之光-第1章-Android新特性-读书笔记

    第 1 章 Android 新特性 1.1 Android 5.0 新特性 1.1.1 Android 5.0 主要新特性 1. 全新的 Material Design 新风格 Material De ...

  6. Android O 可以上网 提示无法访问网络

    android O连接Wifi,可以上网,但是却提示无法访问网络,并且在wifi图标上有一个'x'. 从android N开始引入了监控机制,每次连接都会访问一下google的服务器,由于国内被墙,所 ...

  7. rxjava 调用retrofit执行网络请求的过程

    retrofit流程图 -1.RxJava调用Retrofit,从requestGtPushSaeUserInfo()中获得被观察者observable,然后new一个观察者向它订阅   0.从业务中 ...

  8. 《Android进阶之光》--网络编程与网络框架

    No1: Volley源码分析: Volley.newRequestQueue-> RequestQueue.start()-> CacheDispatcher.start()->C ...

  9. 《Android进阶之光》--RxJava实现RxBus

    事件总线RxBus,替代EventBus和otto 1)创建RxBus public class RxBus{ private static volatile RxBus rxBus; private ...

随机推荐

  1. yolov3实践(一)

    很多博友看了我的第一篇博客yolo类检测算法解析——yolo v3,对其有了一定的认识和了解,但是并没有贴出代码和运行效果,略显苍白.因此在把篇博客理论的基础上,造就了第一篇实践文章,也就是本文.只要 ...

  2. Java 文本I/O 处理

    File类包含获得一个文件/目录的属性,以及对文件/目录进行改名和删除的方法. File类包含许多获取文件属性的方法,以及重命名和删除文件和目录的方法,但是,File类不包含读写文件内容的方法 Fil ...

  3. cmake介绍

    1. cmake介绍 1.1 cmake用途 CMake的用途是能通过一系列的源码和相关的配置来生成需要的编译器平台上的项目文件.譬如,如果一个项目需要在Windows上用VS编译,在Linux上用m ...

  4. Python爬虫-爬取百度贴吧帖子

    这次主要学习了替换各种标签,规范格式的方法.依然参考博主崔庆才的博客. 1.获取url 某一帖子:https://tieba.baidu.com/p/3138733512?see_lz=1&p ...

  5. js 当前时区

    function formatDateTime(formatDate){ //13位时间戳,java js. (php时间戳为10位) var returnDate; if(formatDate == ...

  6. C语言表驱动法编程实践

    数据压倒一切.如果选择了正确的数据结构并把一切组织的井井有条,正确的算法就不言自明.编程的核心是数据结构,而不是算法. ——Rob Pike  说明 本文基于这样的认识:数据是易变的,逻辑是稳定的. ...

  7. Libevent源码分析—event_add()

    接下来就是将已经初始化的event注册到libevent的事件链表上,通过event_add()来实现,源码位于event.c中. event_add() 这个函数主要完成了下面几件事: 1.将eve ...

  8. 【sky第二期--PID算法】--【智能车论坛】

    [sky第二期--PID算法] 想学PID的可以来[智能车论坛]这里有我发布的资料http://bbs.tekbots.eefocus.com/forum.php?mod=viewthread& ...

  9. Python3学习笔记17-类与实例

    面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类 而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 ...

  10. 基于url拦截