From 514dd5173aae0373474920498e9f1923ff281b65 Mon Sep 17 00:00:00 2001 From: Invinciblelee <1760316362@qq.com> Date: Fri, 24 May 2019 12:54:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0okhttp,=20retrofit=E9=85=8D?= =?UTF-8?q?=E5=90=88=E5=8D=8F=E7=A8=8B=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/base/BaseViewModel.kt | 22 ++++++---------- .../io/legado/app/data/api/CommonHttpApi.kt | 14 +++++++++++ ...ory.kt => CoroutinesCallAdapterFactory.kt} | 4 +-- .../io/legado/app/help/http/HttpHelper.kt | 15 +++++++++++ .../legado/app/ui/search/SearchViewModel.kt | 25 +++++++++++++++++-- 5 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/io/legado/app/data/api/CommonHttpApi.kt rename app/src/main/java/io/legado/app/help/http/{CoroutineCallAdapterFactory.kt => CoroutinesCallAdapterFactory.kt} (95%) diff --git a/app/src/main/java/io/legado/app/base/BaseViewModel.kt b/app/src/main/java/io/legado/app/base/BaseViewModel.kt index 009aafe7d..74bc4bfc2 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -13,13 +13,12 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio private val launchManager: MutableList = mutableListOf() protected fun launchOnUI( - tryBlock: suspend CoroutineScope.() -> Unit, - cacheBlock: suspend CoroutineScope.(Throwable) -> Unit, - finallyBlock: suspend CoroutineScope.() -> Unit, - handleCancellationExceptionManually: Boolean + tryBlock: suspend CoroutineScope.() -> Unit,//成功 + errorBlock: suspend CoroutineScope.(Throwable) -> Unit,//失败 + finallyBlock: suspend CoroutineScope.() -> Unit//结束 ) { launchOnUI { - tryCatch(tryBlock, cacheBlock, finallyBlock, handleCancellationExceptionManually) + tryCatch(tryBlock, errorBlock, finallyBlock) } } @@ -27,25 +26,20 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio * add launch task to [launchManager] */ private fun launchOnUI(block: suspend CoroutineScope.() -> Unit) { - val job = launch { block() } + val job = launch { block() }//主线程 launchManager.add(job) job.invokeOnCompletion { launchManager.remove(job) } } private suspend fun tryCatch( tryBlock: suspend CoroutineScope.() -> Unit, - catchBlock: suspend CoroutineScope.(Throwable) -> Unit, - finallyBlock: suspend CoroutineScope.() -> Unit, - handleCancellationExceptionManually: Boolean = false + errorBlock: suspend CoroutineScope.(Throwable) -> Unit, + finallyBlock: suspend CoroutineScope.() -> Unit ) { try { coroutineScope { tryBlock() } } catch (e: Throwable) { - if (e !is CancellationException || handleCancellationExceptionManually) { - coroutineScope { catchBlock(e) } - } else { - throw e - } + coroutineScope { errorBlock(e) } } finally { coroutineScope { finallyBlock() } } diff --git a/app/src/main/java/io/legado/app/data/api/CommonHttpApi.kt b/app/src/main/java/io/legado/app/data/api/CommonHttpApi.kt new file mode 100644 index 000000000..08e06f52f --- /dev/null +++ b/app/src/main/java/io/legado/app/data/api/CommonHttpApi.kt @@ -0,0 +1,14 @@ +package io.legado.app.data.api + +import kotlinx.coroutines.Deferred +import retrofit2.http.* + +interface CommonHttpApi { + + @GET + fun get(@Url url: String, @QueryMap map: Map): Deferred + + @FormUrlEncoded + @POST + fun post(@Url url: String, @FieldMap map: Map): Deferred +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/CoroutineCallAdapterFactory.kt b/app/src/main/java/io/legado/app/help/http/CoroutinesCallAdapterFactory.kt similarity index 95% rename from app/src/main/java/io/legado/app/help/http/CoroutineCallAdapterFactory.kt rename to app/src/main/java/io/legado/app/help/http/CoroutinesCallAdapterFactory.kt index 78e0aa7a0..bf296f5bb 100644 --- a/app/src/main/java/io/legado/app/help/http/CoroutineCallAdapterFactory.kt +++ b/app/src/main/java/io/legado/app/help/http/CoroutinesCallAdapterFactory.kt @@ -6,10 +6,10 @@ import retrofit2.* import java.lang.reflect.ParameterizedType import java.lang.reflect.Type -class CoroutineCallAdapterFactory private constructor() : CallAdapter.Factory() { +class CoroutinesCallAdapterFactory private constructor() : CallAdapter.Factory() { companion object { @JvmStatic @JvmName("create") - operator fun invoke() = CoroutineCallAdapterFactory() + operator fun invoke() = CoroutinesCallAdapterFactory() } override fun get( diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 1ef3b7d7b..59d92b07d 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -1,6 +1,7 @@ package io.legado.app.help.http import okhttp3.* +import retrofit2.Retrofit import java.util.* import java.util.concurrent.TimeUnit @@ -9,6 +10,20 @@ object HttpHelper { val client: OkHttpClient = getOkHttpClient() + fun getApiService(baseUrl: String, clazz: Class): T { + return getRetrofit(baseUrl).create(clazz) + } + + fun getRetrofit(baseUrl: String): Retrofit { + return Retrofit.Builder().baseUrl(baseUrl) + //增加返回值为字符串的支持(以实体类返回) +// .addConverterFactory(EncodeConverter.create()) + //增加返回值为Observable的支持 + .addCallAdapterFactory(CoroutinesCallAdapterFactory.invoke()) + .client(client) + .build() + } + private fun getOkHttpClient(): OkHttpClient { val cs = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index fb7b213f7..0f9f2072b 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -1,18 +1,39 @@ package io.legado.app.ui.search import android.app.Application +import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import io.legado.app.base.BaseViewModel +import io.legado.app.data.api.CommonHttpApi import io.legado.app.data.entities.SearchBook +import io.legado.app.help.http.HttpHelper +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.withContext -class SearchViewModel(application: Application) : BaseViewModel(application){ +class SearchViewModel(application: Application) : BaseViewModel(application) { val searchBooks: LiveData> = MutableLiveData() - public fun search(){ + public fun search(start: () -> Unit, finally: () -> Unit) { + launchOnUI( + { + start() + val searchResponse = withContext(IO) { + HttpHelper.getApiService( + "http:www.baidu.com", + CommonHttpApi::class.java + ).get("", mutableMapOf()) + } + val result = searchResponse.await() + }, + { Log.i("TAG", "${it.message}") }, + { finally() }) +// GlobalScope.launch { +// +// } } }