Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i tried to parse json data, but it kind of weird because it not show the right data but if i tried to called json on browser it has the right data.

so this is how i parse the json data

doAsync {
        val url = localhost.getMovie()
        val request = okhttp3.Request.Builder().url(url).build()
        val client = OkHttpClient()

        uiThread {
            client.newCall(request).enqueue(object : Callback, okhttp3.Callback {
                override fun onResponse(call: okhttp3.Call?, response: okhttp3.Response?) {
                    val body = response?.body()?.string()
                    println(body)
                    uiThread {
                        val gson = GsonBuilder().create()
                        val movieFeed = gson.fromJson(body, Movie2Response::class.java)

                        Log.v("body", ""+body)
                        Log.v("feed", ""+movieFeed.data)

                        uiThread {
                        }
                    }
                }

                override fun onFailure(call: okhttp3.Call?, e: IOException) {
                    println("failed")
                }

            })
        }
    }

movie response

class Movie2Response (val data: MutableList<Movie2>)

movie

class Movie2 (
    @SerializedName("id")
    var movieId: String? = null,
    @SerializedName("description")
    var synopsis: String? = null,
    @SerializedName("release_date")
    var release: String? = null,
    @SerializedName("poster")
    var poster: String? = null,
    @SerializedName("genre")
    var genre: String? = null,
    @SerializedName("title")
    var title: String? = null

)

and this is what i got from the json data

 V/body: {"data":[{"title":"Aquaman","description":""........
V/feed: [com.mqa.android.moviereview.model.Movie2@7509e04, com.mqa.android.moviereview.model.Movie2@890afed, com.mqa.android.moviereview.model.Movie2@9834e22, com.mqa.android.moviereview.model.Movie2@f02d0b3, com.mqa.android.moviereview.model.Movie2@d3b9670, com.mqa.android.moviereview.model.Movie2@4d55de9, com.mqa.android.moviereview.model.Movie2@cac2a6e, com.mqa.android.moviereview.model.Movie2@94fc50f, com.mqa.android.moviereview.model.Movie2@d9ba99c]

it shows right in body but in the array it show like that. please help what is wrong with it. because i want to show the title data to the spinner

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
205 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your code worked pretty well as the log results showed. The real problem is the log function Log.v("feed", ""+movieFeed.data). If you want to show pretty log, you should override the toString() method in Movie2 class by:

Open Movie2 and right click in the editor -> Generate -> then click toString() to override it.

For data class in Kotlin, you can just add data before class keyword.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...