compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//Retrofit2所需要的包 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//ConverterFactory的Gson依赖包 compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'//ConverterFactory的String依赖包
//Rxjava compile 'io.reactivex:rxandroid:1.2.0' compile 'io.reactivex:rxjava:1.2.0' //Retrofit compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0' compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'
retrofit = Retrofit.Builder()
.client(build.build())
.baseUrl("你的url")
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build().create(RetrofitUrl::class.java)
val build = OkHttpClient.Builder().connectTimeout(15,TimeUnit.SECONDS)
.writeTimeout(15,TimeUnit.SECONDS)
.readTimeout(15,TimeUnit.SECONDS)
val logging = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger {
Log.e("retrofit url",it)
})
logging.level = HttpLoggingInterceptor.Level.BODY
val gson = GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create()
interface RetrofitUrl {
//方法名自定义
@GET("接口地址")
fun load():Observable<对应实体类>
@FormUrlEncoded
@Post("接口地址")
fun load():Observable<对应实体类>
//需要传递参数,多个参数逗号隔开
@GET("接口地址")
fun load(@Query("参数名字") 参数名字(可自定义):参数类型):Observable<对应实体类>
@FormUrlEncoded
@Post("接口地址")
fun load(@Field("参数名字") 参数名字(可自定义):参数类型):Observable<对应实体类>
//示例
@GET("load")
fun load():Observable<NetOuter<Orgs>>
@GET("load")
fun load(@Query("id") id:Int):Observable<NetOuter<Orgs>>
}
retrofit().load()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object :Subscriber<NetOuter<Orgs>>(){
override fun onCompleted() {
}
override fun onNext(t: NetOuter<Orgs>?) {
//可以在这里对获取到的数据进行处理
}
override fun onError(e: Throwable?) {
//请求失败
}
)
fun <T> runRx(observable: Observable<T>, subscriber: Subscriber<T>): Subscription = observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(subscriber)
runRx(retrofit().load(),object : Subscriber<NetOuter<Orgs>>() {
override fun onCompleted() {}
override fun onNext(t: NetOuter<Orgs>?) {
//可以在这里对获取到的数据进行处理
}
override fun onError(e: Throwable?) {
//请求失败
}
)
fun <T> runRxLambda(observable: Observable<T>,next:(T)->Unit,error:(e: Throwable?)->Unit,completed:() -> Unit = { Log.e("completed","completed") }){
runRx(observable, object : Subscriber<T>() {
override fun onCompleted() { completed }
override fun onNext(t: T) { next(t) }
override fun onError(e: Throwable?) { error(e) }
})
}
runRxLambda(retrofit().load(),{
//我们在这里的操作就相当于在onNext中的操作,参数可以通过it获取
},{
//这里就是onError的实现,参数也可以通过it获取
})
runRxLambda(retrofit().load(),{
//我们在这里的操作就相当于在onNext中的操作,参数可以通过it获取
},{
//这里就是onError的实现,参数也可以通过it获取
},{
//这里是onCompleted,不实现也可以
})
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有