1、引入需要的网络库
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.3'
2、编写RetrofitUtils类,可以切换多个请求地址具体根据自己的项目需求
import com.google.gson.Gson
import okhttp3.OkHttpClient
import okhttp3.Protocol
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.*
import java.util.concurrent.TimeUnit
class RetrofitUtils{
companion object{
private var url=""
fun <T> configRetrofit(type: Int,service: Class<T>): T{
if(type==1){
url="https://"//网络地址1
}else if(type==2){
url="https://"//网络地址2
}
val client = OkHttpClient().newBuilder()
.connectTimeout(10,TimeUnit.SECONDS)
.readTimeout(10,TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.addNetworkInterceptor(LoggingInterceptor())
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
.build()
val retrofit = Retrofit.Builder().baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(Gson()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build()
return retrofit.create(service)
}
}
}
3、编写网络Log过滤器
package com.neusoft.myapplication.net
import android.util.Log
import android.util.Log.i
import okhttp3.Interceptor
import okhttp3.Response
class LoggingInterceptor: Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val t1 = System.nanoTime()
Log.i("LogginInterceptor","发送请求"+request.url()+chain.connection()+request.headers())
//发送请求
val response = chain.proceed(request)
//收到响应时间
val t2 =System.nanoTime()
//这里不能直接使用response.body().string()的方式输出日志
//因为response.body().string()之后,response中的流会被关闭,程序会报错,我们需要创建出
//一个新的response给应用层处理
val responseBody = response.peekBody(1024 * 1024)
info("LogginInterceptor","接收响应"+response.request().url()+responseBody.string()+(t2-t1) / 1e6)
return response
}
private fun info(tag: String,bodyMsg: String){
if(bodyMsg.length>4000){
for(index in 0..bodyMsg.length){
//当前截取的长度,总长度则继续截取最大的长度来打印
if(index+4000<bodyMsg.length){
Log.i(tag+index,bodyMsg.substring(index,index+4000))
}else{
Log.i(tag+index,bodyMsg.substring(index))
}
}
}else{
Log.i(tag,bodyMsg)
}
}
}
4、编写网络回调接口
public interface CallBack<T>{
fun onSuccess(data: T)
fun onFail(code: Int, data: String?)
}
5、编写BaseObeserver统一处理数据
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
open class BaseObserver<T>(callBack:CallBack<T>) : Observer<T>{
var callBack : CallBack<T>?=null//初始化
init {
this.callBack = callBack;
}
override fun onComplete() {
}
override fun onSubscribe(d: Disposable) {
}
override fun onNext(t: T) {
if(t!=null){
callBack!!.onSuccess(t)//判断callBack不为空
}else{
callBack!!.onFail(999,"获取数据失败")//判断callBack不为空
}
}
override fun onError(e: Throwable) {
callBack!!.onFail(1000,e.message)
}
}
未完待续...