Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read JSON from Url using kotlin Android?

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?

like image 209
sasikumar Avatar asked Jul 03 '17 10:07

sasikumar


4 Answers

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

like image 103
Daniel Reina Avatar answered Oct 21 '22 14:10

Daniel Reina


Update / Note: I've written an entire step by step tutorial on this topic, here. While it's up to you what third party libraries to use (there are some mentioned in the other answers), Retrofit is by far the defacto standard being used these days. Also, it has built in support for coroutines. Your networking code should be asynchronous and your code will be much cleaner, easier to follow and debug, etc. if you use coroutines rather than the old callback methodology. The aforementioned tutorial even shows you how to easily update old code from callbacks to use coroutines, instead.

readText() is the approach Antonio Leiva uses when he introduces Networking in his book Kotlin for Android Developers (and also in the blog post as per dgrcode's answer) However I'm not sure if readText() is the right approach in general for requesting JSON responses from a URL as the response can be very large (Even Antonio mentions affirms the fact that Kotlin docs say there is a size limit to readText). According to this discussion, you should be using streams instead of readText. I would like to see a response to this answer using that approach or whatever is most concise and optimal in Kotlin, without adding library dependencies.

See also this thread

Note also that Antonio does offer alternatives in that blog post for situations when readText() is not the appropriate choice. I'd love to see someone provide some clarity on when it is appropriate or not though.

like image 27
Fuad Kamal Avatar answered Oct 21 '22 15:10

Fuad Kamal


Try this

fun parse(json: String): JSONObject? {
        var jsonObject: JSONObject? = null
        try {
            jsonObject = JSONObject(json)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        return jsonObject
    }
like image 4
Ankit Khare Avatar answered Oct 21 '22 14:10

Ankit Khare


Finally am getting answer from Here

Read Json data using Retrofit 2.0 RxJava, RxAndroid, Kotlin Android Extensions.

  fun provideRetrofit(): Retrofit {
    return Retrofit.Builder()
            .baseUrl("https://www.example.com")
            .addConverterFactory(MoshiConverterFactory.create())
            .build()
    }

Module

interface  Api {
@GET("/top.json")
fun getTop(@Query("after") after: String,
           @Query("limit") limit: String): Call<NewsResponse>;
}
like image 4
sasikumar Avatar answered Oct 21 '22 15:10

sasikumar