importretrofit2.Retrofit; importretrofit2.adapter.rxjava.RxJavaCallAdapterFactory; importretrofit2.converter.gson.GsonConverterFactory; importrx.Subscriber; importrx.android.schedulers.AndroidSchedulers; importrx.schedulers.Schedulers; /** * Created by wangjitao on 2016/11/3 0003. *对Retrofit的的简单...
去年的时候学习了Rxjava和Retrofit的基本用法,但一直没有在实际项目中运用。今年开做新项目,果断在新项目中引入了RxJava和Retrofit。本篇文章将介绍笔者在项目中对Retrofit的封装。先来看一下封装过后的Retrofit如何使用。
RxJava + Retrofit + okHttp 已成为当前Android 网络请求最流行的方式。 一、添加依赖库 代码语言:txt AI代码解释 //RxJava compile 'io.reactivex:rxjava:1.1.3' //RxAndroid compile 'io.reactivex:rxandroid:1.1.0' //retrofit compile 'com.squareup.retrofit2:retrofit:2.0.0' //retrofit依赖Gson comp...
首先,在你的项目的build.gradle文件中添加如下依赖库: implementation'io.reactivex.rxjava2:rxjava:2.2.19'implementation'io.reactivex.rxjava2:rxandroid:2.1.1'implementation'com.squareup.retrofit2:retrofit:2.9.0'implementation'com.squareup.retrofit2:converter-gson:2.9.0' 1. 2. 3. 4. 3.2 创建Retro...
在Retrofit中使用RxJava 上次我们获取手机的归属地时的PhoneService中是这样写的: [代码]java代码: 1 2 3 @GET("/apistore/mobilenumber/mobilenumber") Call<phoneresult> getResult(@Header("apikey") String apikey, @Query("phone") String phone);</phoneresult> 返回了一个Call对象,使用RxJava我们则...
compile 'io.reactivex.rxjava2:rxjava:2.0.7' // 注:RxJava2 与 RxJava1 不能共存,即依赖不能同时存在 } 1. 2. 3. 4. 5. 3.1 Map() 作用 对 被观察者发送的每1个事件都通过指定的函数处理,从而变换成另外一种事件 即,将被观察者发送的事件转换为任意的类型事件。
implementation 'com.squareup.retrofit2:adapter-rxjava3:2.x.y'} 记得将3.x.y和2.x.y替换成实际的版本号。创建Retrofit实例 使用Retrofit创建网络请求实例时,需要指定接口的地址、转换器和适配器。例如:java public interface ApiService { GET("users/{user}")Single getUser(@Path("user") ...
其中,3.x.y是指RxJava3和rxandroid的版本号,2.x.y是指Retrofit2的版本号。2. 创建Retrofit实例 ...
一般一个接口只是一次请求一次返回,所以使用Single 与 Retrofit 配合是更为合理; Single.just(1) .map { "num_$it" } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { num -> LjyLogUtil.d(num) } Single.create(SingleOnSubscribe<String> { emitter -> LjyLogUt...
compile'io.reactivex:rxjava:1.1.0'compile'io.reactivex:rxandroid:1.1.0'compile'com.squareup.retrofit2:retrofit:2.0.0-beta4'compile'com.squareup.retrofit2:converter-gson:2.0.0-beta4'compile'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4' ...