Skip to content

Commit

Permalink
更新了使用方法
Browse files Browse the repository at this point in the history
  • Loading branch information
allens committed Jun 23, 2020
1 parent dc17400 commit b9bcd5e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 54 deletions.
36 changes: 20 additions & 16 deletions app/src/main/java/com/allens/rxhttp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
.connectTimeout(10)
.addBuilderClientListener(object : OnBuildClientListener {
override fun addBuildClient(): MutableSet<Any> {
return mutableSetOf(GsonConverterFactory.create(),RxJava2CallAdapterFactory.create())
return mutableSetOf(
GsonConverterFactory.create(),
RxJava2CallAdapterFactory.create()
)
}
})
.build(this)
Expand All @@ -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()
}
)
}
}

Expand All @@ -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()
})
}
}
}
13 changes: 0 additions & 13 deletions lib_http-coroutine/src/main/java/com/allens/lib_http2/RxHttp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,6 @@ class RxHttp {
return RequestBuilder()
}


inline fun <T : Any> checkResult(
result: HttpResult<T>,
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,18 @@ sealed class HttpResult<out T : Any> {
is Error -> "Error[throwable=$throwable]"
}
}

inline fun result(
success: (T) -> Unit,
error: (Throwable) -> Unit
) {
when (this) {
is Success -> {
success(data)
}
is Error -> {
error(throwable)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

Expand All @@ -200,17 +201,4 @@ class RequestBuilder {
UpLoadPool.remove(tag)
}


private inline fun <T : Any> checkResult(
result: HttpResult<T>,
success: (T) -> Unit,
error: (Throwable) -> Unit
) {
if (result is HttpResult.Success) {
success(result.data)
} else if (result is HttpResult.Error) {
error(result.throwable)
}
}

}

0 comments on commit b9bcd5e

Please sign in to comment.