《Android进阶之光》--RxJava结合Retrofit访问网络
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访问网络的更多相关文章
- 《Android进阶之光》--RxJava
No1: RxJava使用 dependencies{ compile 'io.reactivex:rxjava:1.2.0' compile 'io.reactivex:rxandroid:1.2. ...
- [置顶]
曙光到来,我的新书《Android进阶之光》已出版
独立博客版本请点击这里 由来 2016年我开始建立了自己的知识体系,所有的文章都是围绕着这个体系来写,随着这个体系的慢慢成长,开始有很多出版社联系我写书,因为比较看好电子工业出版社,就顺理成章的开始了 ...
- 《Android进阶之光》--注解与依赖注入框架
No1: 标准注解: 1)@Override:覆写 2)@Deprecated:过时 3)@SuppressWarnings:取消警告 4)@SafeVarargs:申明使用了可变长度参数的方法 No ...
- 《Android进阶之光》--Material Design
接上篇<Android进阶之光>--Android新特性 No1: 组件: 1)底部工作条-Bottom Sheets 2)卡片-Cards 3)提示框-Dialogs 4)菜单-Menu ...
- Android进阶之光-第1章-Android新特性-读书笔记
第 1 章 Android 新特性 1.1 Android 5.0 新特性 1.1.1 Android 5.0 主要新特性 1. 全新的 Material Design 新风格 Material De ...
- Android O 可以上网 提示无法访问网络
android O连接Wifi,可以上网,但是却提示无法访问网络,并且在wifi图标上有一个'x'. 从android N开始引入了监控机制,每次连接都会访问一下google的服务器,由于国内被墙,所 ...
- rxjava 调用retrofit执行网络请求的过程
retrofit流程图 -1.RxJava调用Retrofit,从requestGtPushSaeUserInfo()中获得被观察者observable,然后new一个观察者向它订阅 0.从业务中 ...
- 《Android进阶之光》--网络编程与网络框架
No1: Volley源码分析: Volley.newRequestQueue-> RequestQueue.start()-> CacheDispatcher.start()->C ...
- 《Android进阶之光》--RxJava实现RxBus
事件总线RxBus,替代EventBus和otto 1)创建RxBus public class RxBus{ private static volatile RxBus rxBus; private ...
随机推荐
- JavaScript之DOM概念
一.DOM概念 1.DOM是什么? 1.1 起源.DOM起源于Netscape与Microsoft 公司的DHTML(动态HTML). 1.2 名词解释.Document Object Model,文 ...
- 第16月第23天 atos
1. grep --after-context=2 "Binary Images:" *crash xcrun atos -o zhiniao_adhoc_stg1.app.dSY ...
- mod_wsgi 的两种模式
mod_wsgi 的两种模式 http://ssmax.net/archives/977.html http://www.cnblogs.com/yuxc/p/3555005.html mod_wsg ...
- Biorhythms HDU - 1370 (中国剩余定理)
孙子定理: 当前存在三个式子,t%3=2,t%5=3,t%7=2.然后让你求出t的值的一个通解. 具体过程:选取3和5的一个公倍数t1能够使得这个公倍数t1%7==1,然后选取3和7的一个公倍数t2使 ...
- 2018-2019-2 网络对抗技术 20165320 Exp2 后门原理与实践
后门原理与实践 windows获取Linux操作Shell 获取本机的IP netcat介绍:一个进行基本的TCP.UDP数据收发的工具 相关的参数与具体用法 windows打开监听: ncat.ex ...
- SRS+flv.js打造兼容性较高的直播、点播平台
**************************************************************************************************** ...
- k64 datasheet学习笔记3---Chip Configuration之System modules
1.前言 本文主要介绍芯片配置的系统模块的内容 2.SIM配置 TODO 3.SMC配置 TODO 4.PMC配置 TODO 5.LOW-LEAKAGE WAKEUP单元配置 TODO 6.MCM配置 ...
- MyEclipse2017 CI-7的破解
下载了一个最新版的MyEclipse,网上下载了破解工具,按照步骤完成后破解失败.很纳闷,于是网上查看,说是破解器的版本须与MyEclipse的版本对应,不对应的话,是没有效果的.如我的是CI-7版本 ...
- odoo - context
得到整个context self.context_get() self.env['res.users'].context_get() 得到context里面对应的值 eg:得到flag的值 self. ...
- 一步步实现windows版ijkplayer系列文章之七——终结篇(附源码)
一步步实现windows版ijkplayer系列文章之一--Windows10平台编译ffmpeg 4.0.2,生成ffplay 一步步实现windows版ijkplayer系列文章之二--Ijkpl ...