《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 ...
随机推荐
- android 基础题
1. Android的四大组件是哪些,它们的作用? (1).Activity:Activity是Android程序与用户交互的窗口,是Android构造块中最基本的一种,它需要为保持各界面的状态,做很 ...
- 记一次Win Server 2012部署问题及解决方法
1.前言 本章内容为在win server 2012服务器部署时遇到的问题及解决方法.大致工作为:两台服务器,一台web.一台数据库:web服务器部署.net web程序,数据库服务器安装oracle ...
- 用conda管理Python包
用conda管理Python包 conda是一个很好的包管理工具,在用了Anaconda之后一直不知道怎么用conda进行管理,其实很简单,就是没人教,慢慢自己摸索了一点.直接在Anaconda的命令 ...
- bzoj 1803: Spoj1487 Query on a tree III(主席树)
题意 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点) 假设没有两个相同的点权. 输入格式: 第一行为整数n, ...
- Vue.js——循环(Java、JSTL标签库、数据库)
一.Vue.js循环 Vue.js循环要使用 v-for 指令. v-for 指令需要以 student in StudentList 形式的特殊语法使用, StudentList 是源数据数组并且s ...
- Day4--------------对文件的权限管理
一.文件权限 ls -l 显示当前文件详细信息 例: -rw-r--rwx.1 root root 1415 11月 9 20:21 anaconda-ks.cfg 依次顺序为:权限信 ...
- mysql 常用,使用经验
mysql default boolean字段 `enable` char(1) NOT NULL DEFAULT '1' COMMENT '启(禁)用',结果: this.enable ? &qu ...
- 关卡得分(if 嵌套for)与(for嵌套if)
- Github之协同开发
一.协同开发 1.引子:假如三个人共同开发同一份代码,每个人都各自安排了任务,当每个人都完成了一半的时候,提交不提交呢? 要提交,提交到dev吗,都上传了一半,这样回家拿出来的代码根本跑不起来.所以, ...
- java util Aarrys 类
toString方法 产生一维数组的二维版本 public static String toString(type[] a) { //type 是传入数据类型 if (a == null) retu ...