《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 ...
随机推荐
- SEO之robots.txt
[关键词:robot.txt,sitemap,User-Agent,Disallow,Allow][声明:摘自Wikipedia] 1. 定义:robots.txt(统一小写)是一种存放于网站根目录下 ...
- python 多线程中子线程和主线程相互通信
主线程开启多个线程去干活,每个线程需要完成的时间不同,干完活以后都要通知给主线程,下面代码说明该应用: 代码块: import threading import queue import time i ...
- mysql数据库备份和恢复
1.数据库备份 mysqldump -uroot -proot jira736 > jira736.sql 2.数据库恢复 mysql -uroot -proot jira762 < ji ...
- MySQL复制框架
一.复制框架 开始接触复制时,看到各种各样的复制,总想把不同类型对应起来,结果越理越乱~究其原因就是对比了不同维度的属性,不同维度得出的结果集之间必然存在交集,没有必要将不同维度的属性安插到成对的萝卜 ...
- Picasso的使用
相信做Android开发的对Square公司一定不会陌生,大名鼎鼎的网络请求框架Retrofit就来源于它,今天学习的是该公司出品的图片加载框架Picasso. 项目地址 https://github ...
- RabbitMQ运行机制
AMQP中消息的路由过程和Java开发者熟悉的JMS存在一些差别,AMQP中增加了Exchange和Binding的角色,生产者把消息发布到Exchange上,Binding决定发布到Exchange ...
- py-faster-rcnn + opencv3.0.0 + ubuntu16.04配置(CPU模式)
最近开始做行人检测,因此开始接触faster-rcnn,这里贴上配置教程(亲测可行),不过是基于cpu的,蓝瘦... 参考博客:http://www.tuicool.com/articles/nYJr ...
- V4L2应用程序框架【转】
转自:https://www.cnblogs.com/hzhida/archive/2012/05/29/2524397.html V4L2是V4L的升级版本,linux下视频设备程序提供了一套接口规 ...
- linux regulator之浅见【转】
转自:http://blog.csdn.net/batoom/article/details/17081651 1: 校准器的基本概念 所谓校准器实际是在软件控制下把输入的电源调节精心输出. Regu ...
- 用rand()和srand()产生伪随机数的方法总结 【转】
转自:http://blog.chinaunix.net/uid-26722078-id-3754502.html 标准库(被包含于中)提供两个帮助生成伪随机数的函数: 函数一:int rand(vo ...