diff --git a/app/src/main/java/com/allens/rxhttp/MainActivity.kt b/app/src/main/java/com/allens/rxhttp/MainActivity.kt index 41bea9b..1a4e06c 100644 --- a/app/src/main/java/com/allens/rxhttp/MainActivity.kt +++ b/app/src/main/java/com/allens/rxhttp/MainActivity.kt @@ -32,7 +32,10 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() { .connectTimeout(10) .addBuilderClientListener(object : OnBuildClientListener { override fun addBuildClient(): MutableSet { - return mutableSetOf(GsonConverterFactory.create(),RxJava2CallAdapterFactory.create()) + return mutableSetOf( + GsonConverterFactory.create(), + RxJava2CallAdapterFactory.create() + ) } }) .build(this) @@ -54,17 +57,20 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() { private fun postRequest() { launch { - val data = rxHttp + rxHttp .create() .addParameter("title", "123456") .addParameter("author", "123456") .addParameter("link", "123456") .doPost("lg/collect/add/json", TestBean::class.java) - rxHttp.checkResult(data, { - log.text = it.toString() - }, { - log.text = it.toString() - }) + .result( + { + log.text = it.toString() + }, + { + log.text = it.message.toString() + } + ) } } @@ -76,15 +82,13 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() { .create() .addParameter("k", "java") .doGet(parameter = "wxarticle/chapters/json", tClass = TestBean::class.java) - - Log.i(TAG, "收到响应 $data thread ${Thread.currentThread().name}") - rxHttp.checkResult(data, { - Log.i(TAG, "success ${Thread.currentThread().name} info $it ") - log.text = it.toString() - }, { - Log.i(TAG, "error ${Thread.currentThread().name} info ${it.toString()} ") - log.text = it.toString() - }) + .result({ + Log.i(TAG, "success ${Thread.currentThread().name} info $it ") + log.text = it.toString() + }, { + Log.i(TAG, "error ${Thread.currentThread().name} info ${it.toString()} ") + log.text = it.toString() + }) } } } diff --git a/lib_http-coroutine/src/main/java/com/allens/lib_http2/RxHttp.kt b/lib_http-coroutine/src/main/java/com/allens/lib_http2/RxHttp.kt index c4c809d..7cfa12a 100644 --- a/lib_http-coroutine/src/main/java/com/allens/lib_http2/RxHttp.kt +++ b/lib_http-coroutine/src/main/java/com/allens/lib_http2/RxHttp.kt @@ -157,19 +157,6 @@ class RxHttp { return RequestBuilder() } - - inline fun checkResult( - result: HttpResult, - success: (T) -> Unit, - error: (String?) -> Unit - ) { - if (result is HttpResult.Success) { - success(result.data) - } else if (result is HttpResult.Error) { - error(result.throwable.message) - } - } - //格式化小数 fun bytes2kb(bytes: Long): String { return FileTool.bytes2kb(bytes) diff --git a/lib_http-coroutine/src/main/java/com/allens/lib_http2/core/HttpResult.kt b/lib_http-coroutine/src/main/java/com/allens/lib_http2/core/HttpResult.kt index e8aa427..319eaa0 100644 --- a/lib_http-coroutine/src/main/java/com/allens/lib_http2/core/HttpResult.kt +++ b/lib_http-coroutine/src/main/java/com/allens/lib_http2/core/HttpResult.kt @@ -11,4 +11,18 @@ sealed class HttpResult { is Error -> "Error[throwable=$throwable]" } } + + inline fun result( + success: (T) -> Unit, + error: (Throwable) -> Unit + ) { + when (this) { + is Success -> { + success(data) + } + is Error -> { + error(throwable) + } + } + } } \ No newline at end of file diff --git a/lib_http-coroutine/src/main/java/com/allens/lib_http2/tools/RequestBuilder.kt b/lib_http-coroutine/src/main/java/com/allens/lib_http2/tools/RequestBuilder.kt index f8a365e..6e68d4d 100644 --- a/lib_http-coroutine/src/main/java/com/allens/lib_http2/tools/RequestBuilder.kt +++ b/lib_http-coroutine/src/main/java/com/allens/lib_http2/tools/RequestBuilder.kt @@ -180,18 +180,19 @@ class RequestBuilder { listener.opUploadPrepare(tag) } val doUpload = doUpload(url, tClass) - checkResult(doUpload, { - withContext(Dispatchers.Main) { - listener.onUpLoadSuccess(tag, it) - } - - UpLoadPool.remove(tag) - }, { - withContext(Dispatchers.Main) { - listener.onUpLoadFailed(tag, throwable = it) - } - UpLoadPool.remove(tag) - }) + .result( + { + withContext(Dispatchers.Main) { + listener.onUpLoadSuccess(tag, it) + } + + UpLoadPool.remove(tag) + }, { + withContext(Dispatchers.Main) { + listener.onUpLoadFailed(tag, throwable = it) + } + UpLoadPool.remove(tag) + }) } } @@ -200,17 +201,4 @@ class RequestBuilder { UpLoadPool.remove(tag) } - - private inline fun checkResult( - result: HttpResult, - success: (T) -> Unit, - error: (Throwable) -> Unit - ) { - if (result is HttpResult.Success) { - success(result.data) - } else if (result is HttpResult.Error) { - error(result.throwable) - } - } - } \ No newline at end of file