DEV Community

Cover image for Kine — Android/Kotlin Networking in 2020
AnkitAgrawal967
AnkitAgrawal967

Posted on

Kine — Android/Kotlin Networking in 2020

Kine — Android/Kotlin Networking in 2020

Wait what? another networking library? don’t we have enough networking library already, I am sure you must be thinking that, you will found out soon enough why

Kine is a 100% pure kotlin networking library that is crazy extendable unlike most other library.

Kine even allows you choose a HttpClient of your choice , a feature retrofit used to provide before 2.x.

Kine supports gson,moshi, okhttp 4x,okhttp 3x,rxjava2, rxjava3, coroutine with ability to support any other library or plugin easily

Let us see some examples :-

For get request:-

val response = "https://example/api/test".httpGet().responseAs(String::class.java)

That’s it, a one liner for a simple get synchronous request

What about asynchronous?

"https://example/api/test".httpGet().responseAs(String::class.java,{ response->
val response = response.body
}, { e ->
e.printStackTrace()
})

As simple as synchronous request

What about POST and other?

"https://example/api/test".httpPost(JSONObject().put("name", "yodo").put("job", "test")).responseAs(JSONObject::class.java, { response ->
responseTextView.text = response.body.toString()
}, { e ->
e.printStackTrace()
responseTextView.text = e.message()
})

That’s it

What about RxJava?, Here you go:-

"https://example/api/test".httpGet().toSingle(JsonObject::class.java)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object:SingleObserver<KineResponse<JsonObject>?> {
override fun onSubscribe(d: Disposable) {
}
override fun onSuccess(response: KineResponse<JsonObject>) {
val response = response.body.toString()
}
override fun onError(e: Throwable) {
e.printStackTrace()
}
})

Coroutines?

GlobalScope.launch(Dispatchers.Main) {
val response = "https://example/api/test".httpGet().responseAsCoroutine(JsonObject::class.java)
}

Don’t use GlobalScope in real application , you appropriate scope

Load Bitmap?

"https://example/api/test/files/abc.png".loadBitmapResponseFromUrl( { response ->
imageView!!.setImageBitmap(response.body)
}, { e -> e.printStackTrace() })

What if you want to bind to ImageView directly?

imageView.loadImage("https://example/api/test/file/abc.png",placeHolderResId)

What about Gson/Moshi support?

"https://example/api/test".httpGet().responseAs(YourCustomPojoClass::class.java,{ response->
val response = response.body
}, { e ->
e.printStackTrace()
})

Library auto detect if Gson/Moshi kine dependency is there and install the converter for it .

What if we want to use gson/moshi both in our project?

"https://example/api/test".httpGet().converter(GsonConverter()).responseAs(YourCustomPojoClass::class.java,{ response->
val response = response.body
}, { e ->
e.printStackTrace()
})

You can use the individual request converter() method to specify converter for a specific request

What if we have our own gson custom adapters and other customization, how do we specify it?

GsonConverter/MoshiConverter takes a gson/moshi instance as a parameter , you can create your gson/moshi instance and pass it there.

You can check the rest on the Project github page: https://github.com/AnkitAgrawal967/Kine

You can also read this on my personal blog :

https://makingmoneyfromonline.com/2020/08/23/kine-networking-in-2020/

Feel free to comment here or create a new issue on github for feature request or bugs or api suggestion

Top comments (0)