KHttp

Project Url: frendyxzc/KHttp
Introduction: Wrapper of OKHttp3 with Kotlin DSL
More: Author   ReportBugs   
Tags:
OKHttp3-Wrapper-Kotlin-DSL-

Wrapper of OKHttp3 with Kotlin DSL

Usage

1. Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

2. Add the dependency:

dependencies {
    compile 'com.github.frendyxzc:KHttp:0.1.0'
}

3. Add your request as follow:

3.1 Get without callback

Request(this).getNewsList("0", "0")
fun getNewsList(uid: String, cid: String) {
    val url_get = RequestCommon.GET_NEWS_LIST + "&uid=${uid}&type_id=${cid}"

    http {
        url = url_get
        method = "get"
        onSuccess { jsonStr ->
            Log.i("", jsonStr)
        }
        onFail { e ->
            Log.i("", e.message)
        }
    }
}

3.2 Get with callback

Request(this).getNewsList("0", "0", object : Callback<ArrayList<News>> {
    override fun onSuccess(data: ArrayList<News>) {
        Log.i("", "GET = ${data[0].title}")
    }
    override fun onFail(error: String?) {
        Log.i("", "GET error = $error")
    }
})
fun getNewsList(uid: String, cid: String, callback: Callback<ArrayList<News>>) {
    val url_get = RequestCommon.GET_NEWS_LIST + "&uid=${uid}&type_id=${cid}"

    http {
        url = url_get
        method = "get"
        onSuccess { jsonStr ->
            callback.onSuccess(gson.fromJson(jsonStr, RespGetNews::class.java).data)
        }
        onFail { e ->
            callback.onFail(e.message)
        }
    }
}

3.3 Post with callback

Request(this).init(object : Callback<UserID>{
    override fun onSuccess(data: UserID) {
        Log.i("", "POST = ${data.user_id}")
    }
    override fun onFail(error: String?) {
        Log.i("", "POST error = $error")
    }
})
fun init(callback: Callback<UserID>) {
    val url_post = RequestCommon.BASE_URL

    val json = JSONObject()
    json.put("action", "init")
    ...
    val postBody = RequestBody.create(JSON, json.toString())

    http {
        url = url_post
        method = "post"
        body = postBody
        onSuccess { jsonStr ->
            callback.onSuccess(gson.fromJson(jsonStr, RespInit::class.java).data)
        }
        onFail { e ->
            callback.onFail(e.message)
        }
    }
}

3.4 Websocket

Socket {
    url = "ws://10.1.1.105:8080/ws"

    onOpen { webSocket, response ->

    }
    onMessage { webSocket, text ->

    }
    onClosing { webSocket, code, reason ->

    }
    onClosed { webSocket, code, reason ->

    }
    onFailure { webSocket, t, response ->

    }
}

3.5 Upload File

Upload {
    url = "http://10.1.1.105:8080/fileUpload"
    file = File("/storage/sdcard0/DCIM/ImageSelector_20170823_144056.mp4")
    fileKey = "file"

    onRequestProgress { bytesWritten, contentLength ->

    }
    onFailure { err ->

    }
    onResponse { response ->

    }
}

3.6 Download File

Download {
    url = "http://10.1.1.105:8080/file/test.apk"
    filePath = "/storage/sdcard0/DCIM/"
    fileName = "test001.apk"

    onRequestProgress { bytesWritten, contentLength ->

    }
    onFail { e ->

    }
    onSuccess { file ->

    }
}

More info:

Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea