I'm completely new to Kotlin. I want to do a login validation using POST method and to get some information using GET method. I've URL, server Username and Password already of my previous project. I didn't find any proper example project which uses this thing. Anyone please suggest me any working example where I can use GET and POST method in HTTP request
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
If you are using
Kotlin
, you might as well keep your code as succinct as possible. Therun
method turns the receiver intothis
and returns the value of the block.this as HttpURLConnection
creates a smart cast.bufferedReader().readText()
avoids a bunch of boilerplate code.You can also wrap this into an extension function.
And call it like this
Finally, if you are super lazy, you can extend the
String
class instead.And call it like this
You can use kohttp library. It is a Kotlin DSL HTTP client. It supports the features of square.okhttp and provides a clear DSL for them. KoHttp async calls are powered by coroutines.
httpGet
extension functionyou can also use async call with coroutines
or DSL function for more complex requests
You can find more details in docs
To get it with gradle use
Send HTTP POST/GET request with parameters using
HttpURLConnection
:POST with Parameters:
GET with Parameters:
GET and POST using OkHttp
Object serialization and deserialization
Dependencies
Put the following lines in your gradle (app) file. Jackson is optional. You can use it for object serialization and deserialization.
For Android, Volley is a good place to get started. For all platforms, you might also want to check out ktor client.
However, you can use standard libraries you would use in Java. For example, with
HttpURLConnection
you can do:Or simpler:
Maybe the simplest GET
For everybody stuck with NetworkOnMainThreadException for the other solutions: use AsyncTask or, even shorter, (yet still experimental) Coroutines:
If you need to test with plain http don't forget to add to your manifest:
android:usesCleartextTraffic="true"
For the experimental Coroutines you have to add to build.gradle as of 10/10/2018: