Am using kotlin for developing the application.Now i want to get JSON data from server.
In java am implemented Asyntask as well as Rxjava for read JSON from Url . Am also search in google but i cant get proper details for my requirement.
How can i read JSON from Url using kotlin?
I guess you're trying to read the response from a server API, which you expect to be a string with JSON. To do so, you can use:
val apiResponse = URL("yourUrl").readText()
From the docs:
Reads the entire content of this URL as a String using UTF-8 or the specified charset.
This method is not recommended on huge files.
Note that, if you're using this inside an Android activity, the app crashes because you're blocking the main thread with an async call. To avoid the app from crashing, I recommend you use Anko. More precisely, you just have to add the commons dependency –not the entire library–.
You can find more info in this blog post