RxJava + Retrofit

本文详细介绍如何在Android开发中使用RxJava进行异步操作及Retrofit实现网络请求。包括添加必要的依赖库、解决可能出现的问题及具体的代码示例。

一、添加依赖

 1     compile 'io.reactivex:rxandroid:1.2.0'
 2     compile 'io.reactivex:rxjava:1.1.5'
 3     compile 'com.google.code.gson:gson:2.4'
 4     compile 'com.squareup.retrofit2:retrofit:2.0.2'
 5     compile 'com.squareup.retrofit2:converter-gson:2.0.2'
 6     compile 'com.squareup.retrofit2:converter-jackson:2.0.0'
 7     compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
 8     compile 'com.squareup.okhttp3:okhttp:3.0.1'
 9     compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
10     compile 'com.squareup.okio:okio:1.6.0'

二、添加依赖可能出现的错误以及解决

Error:Execution failed for task':retrofitdemo:transformResourcesWithMergeJava

解决方法:

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"

    packagingOptions {
        exclude 'META-INF/NOTICE' 
        exclude 'META-INF/LICENSE' 
        exclude 'META-INF/notice'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license'
        exclude 'META-INF/license.txt'
    }
}

 

三、操作:

(1)延时操作

 1 private void timerOption() {
 2         Observable.timer(3000, TimeUnit.MILLISECONDS)
 3                 .subscribeOn(Schedulers.io())
 4                 .observeOn(AndroidSchedulers.mainThread())
 5                 .subscribe(new Action1<Long>() {
 6                     @Override
 7                     public void call(Long aLong) {
 8                         startActivity(new Intent(SplashActivity.this, NewsActivity.class));
 9                         overridePendingTransition(0, android.R.anim.fade_out);
10                         finish();
11                     }
12                 });
13     }

(2)具体使用:

给 Android 开发者的 RxJava 详解

转载于:https://www.cnblogs.com/aimqqroad-13/p/7267310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值