OkHttp是一个成熟且强大的网络库,在Android源码中已经使用OkHttp替代原先的HttpURLConnection。很多著名的框架例如Picasso、Retrofit也使用OkHttp作为底层框架。在这里我对OkHttp做一下简单的封装,其实封装得有点粗暴只是为了演示如何实现dsl。
import io.reactivex.BackpressureStrategy
import io.reactivex.Flowable
import io.reactivex.schedulers.Schedulers
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import java.util.concurrent.TimeUnit
class RequestWrapper {
var url:String? = null
var method:String? = null
var body: RequestBody? = null
var timeout:Long = 10
internal var _success: (String) -> Unit = { }
internal var _fail: (Throwable) -> Unit = {}
fun onSuccess(onSuccess: (String) -> Unit) {
_success = onSuccess
}
fun onFail(onError: (Throwable) -> Unit) {
_fail = onError
}
}
fun http(init: RequestWrapper.() -> Unit) {
val wrap = RequestWrapper()
wrap.init()
executeForResult(wrap)
}
private fun executeForResult(wrap:RequestWrapper) {
Flowable.create<Response>({
e -> e.onNext(onExecute(wrap))
}, BackpressureStrategy.BUFFER)
.subscribeOn(Schedulers.io())
.subscribe(
{ resp ->
wrap._success(resp.body()!!.string())
},
{ e -> wrap._fail(e) })
}
private fun onExecute(wrap:RequestWrapper): Response? {
var req:Request? = null
when(wrap.method) {
"get","Get","GET" -> req =Request.Builder().url(wrap.url).build()
"post","Post","POST" -> req = Request.Builder().url(wrap.url).post(wrap.body).build()
"put","Put","PUT" -> req = Request.Builder().url(wrap.url).put(wrap.body).build()
"delete","Delete","DELETE" -> req = Request.Builder().url(wrap.url).delete(wrap.body).build()
}
val http = OkHttpClient.Builder().connectTimeout(wrap.timeout, TimeUnit.SECONDS).build()
val resp = http.newCall(req).execute()
return resp
}
封装完OkHttp之后,看看如何来编写get请求
http {
url = "http://www.163.com/"
method = "get"
onSuccess {
string -> L.i(string)
}
onFail {
e -> L.i(e.message)
}
}
是不是很像以前用jquery来写ajax?
post请求也是类似的,只不过多了body
var json = JSONObject()
json.put("xxx","yyyy")
....
val postBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json.toString())
http {
url = "https://......"
method = "post"
body = postBody
onSuccess {
string -> L.json(string)
}
onFail {
e -> L.i(e.message)
}
}