Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.frendyxzc:khttp:0.1.2'
}
dependencies {
implementation("com.github.frendyxzc:khttp:0.1.2")
}
<dependency>
<groupId>com.github.frendyxzc</groupId>
<artifactId>khttp</artifactId>
<version>0.1.2</version>
</dependency>
libraryDependencies += "com.github.frendyxzc" % "khttp" % "0.1.2"
:dependencies [[com.github.frendyxzc/khttp "0.1.2"]]
Wrapper of OKHttp3 with Kotlin DSL
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.frendyxzc:KHttp:0.1.0'
}
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)
}
}
}
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)
}
}
}
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)
}
}
}
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 ->
}
}
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 ->
}
}
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 ->
}
}