diff --git a/app/src/main/java/com/common/commonlibtest/manager/CollectionLoader.kt b/app/src/main/java/com/common/commonlibtest/manager/CollectionLoader.kt index d40764c..fe0d949 100644 --- a/app/src/main/java/com/common/commonlibtest/manager/CollectionLoader.kt +++ b/app/src/main/java/com/common/commonlibtest/manager/CollectionLoader.kt @@ -10,15 +10,10 @@ import retrofit2.http.Headers import retrofit2.http.Path class CollectionLoader(interceptors: List) : - BaseObserve() { - private val api: CollectionApi - - init { - api = initService(CollectionApi::class.java, interceptors) - } + BaseObserve(CollectionApi::class.java, interceptors) { fun getCollections(pageName: Int, callBack: RequestCallBack) { - observe(api.getCollections(pageName), callBack) + observe(api?.getCollections(pageName), callBack) } interface CollectionApi { diff --git a/app/src/main/java/com/common/commonlibtest/manager/InternetLoader.kt b/app/src/main/java/com/common/commonlibtest/manager/InternetLoader.kt index 5d7a9e2..8536dda 100644 --- a/app/src/main/java/com/common/commonlibtest/manager/InternetLoader.kt +++ b/app/src/main/java/com/common/commonlibtest/manager/InternetLoader.kt @@ -11,15 +11,10 @@ import retrofit2.http.Path /** * 网络请求样例 */ -class InternetLoader : BaseObserve() { - var homeApi: HomeApi - - init { - homeApi = initService(HomeApi::class.java) - } +class InternetLoader : BaseObserve(HomeApi::class.java) { fun getHomeArticles(pageName: Int, callBack: RequestCallBack) { - observe(homeApi.getHomeArticles(pageName), callBack) + observe(api?.getHomeArticles(pageName), callBack) } interface HomeApi { diff --git a/app/src/main/java/com/common/commonlibtest/manager/LoginLoader.kt b/app/src/main/java/com/common/commonlibtest/manager/LoginLoader.kt index 3fc01cd..812a471 100644 --- a/app/src/main/java/com/common/commonlibtest/manager/LoginLoader.kt +++ b/app/src/main/java/com/common/commonlibtest/manager/LoginLoader.kt @@ -8,15 +8,10 @@ import okhttp3.Interceptor import retrofit2.http.* class LoginLoader(interceptors: List) : - BaseObserve() { - private val api: LoginApi - - init { - api = initService(LoginApi::class.java, interceptors) - } + BaseObserve(LoginApi::class.java, interceptors) { fun login(userName: String, pwd: String, callBack: RequestCallBack) { - observe(api.login(userName, pwd), callBack) + observe(api?.login(userName, pwd), callBack) } interface LoginApi { diff --git a/commonLib/src/main/java/com/common/commonlib/net/BaseObserve.kt b/commonLib/src/main/java/com/common/commonlib/net/BaseObserve.kt index 1f78ff8..1eae8d9 100644 --- a/commonLib/src/main/java/com/common/commonlib/net/BaseObserve.kt +++ b/commonLib/src/main/java/com/common/commonlib/net/BaseObserve.kt @@ -2,6 +2,7 @@ package com.common.commonlib.net import android.net.ParseException import android.util.Log +import com.common.commonlib.log.Logger import com.common.commonlib.net.callback.RequestCallBack import com.google.gson.JsonParseException import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers @@ -24,6 +25,16 @@ import javax.net.ssl.SSLHandshakeException * @since 2021/7/28 */ open class BaseObserve { + var api: T? = null + + constructor(clazz: Class) { + api = RequestManager.create(clazz) + } + + constructor(clazz: Class, interceptors: List) { + api = RequestManager.create(clazz, interceptors) + } + /** * 初始化服务类 */ @@ -31,6 +42,9 @@ open class BaseObserve { return RequestManager.create(clazz) } + /** + * 初始化服务类 + */ fun initService(clazz: Class, interceptors: List): T { return RequestManager.create(clazz, interceptors) } @@ -40,7 +54,11 @@ open class BaseObserve { * @param observable 可订阅 * @param callBack 请求回调 */ - fun observe(observable: Observable, callBack: RequestCallBack?) { + fun observe(observable: Observable?, callBack: RequestCallBack?) { + if (observable == null) { + Logger.e("BaseObserve", "observe observable is null") + return + } observable .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io())