How to make a KOTLIN list with JSON API bracket lists


#1

Hello,

I am trying to make a list in Kotlin from a JSon API. I did the tutorial from Udemy, and I made the Weather app. It works perfectly, but now I need to make a new list with an orders list.

Here is the Weather API code :

{
“query”: {
“results”: {
“channel”: {
“item” : {
“forecast” : [
{
“data”: 1
},
{
“data”: 1
},
{
“data”: 1
}
]
}
}
}
}
}

But now I need to make a list, and to take the previous example, it looks like this :

{

“query”: {
“results”: {
“channel”: {
“item”: {
“forecast”: {
“0”: {
“data”: 1
},
“1”: {
“data”: 1
},
“2”: {
“data”: 1
}
}
}
}
}
}
}

Here is the Kotlin code that is working with the first example of API :

// IN FILE WeatherHelp.kt

import retrofit2.Call
import retrofit2.Callback
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Query

interface WeatherAPI {
@GET(“yql?format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys”)
fun getForecast(@Query(“q”) q: String) : Call
}

class Weather(val query: WeatherQuery)
class WeatherQuery(val results: WeatherResult)
class WeatherResult(val channel: WeatherChannel)
class WeatherChannel(val title: String, val item: WeatherItem)
class WeatherItem(val forecast: List)
class Forecast(val date: String, val day: String, val high: String, val low: String, val text: String)

class WeatherRetriever {
val service : WeatherAPI

init {
    val retrofit = Retrofit.Builder().baseUrl("https://query.yahooapis.com/v1/public/").addConverterFactory(GsonConverterFactory.create()).build()
    service = retrofit.create(WeatherAPI::class.java)
}

fun getForecast(callback : Callback<Weather>, searchTerm : String) {
    var default = searchTerm
    if (searchTerm == "") {
        default = "Paris"
    }
    val q = "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"${default}\")"
    val call = service.getForecast(q)
    call.enqueue(callback)
}

}

// IN FILE ForecastActivity.kt

class ForecastActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_forecast)

    val retriever = WeatherRetriever()

    val callback = object : Callback<Weather> {
        override fun onResponse(call: Call<Weather>?, response: Response<Weather>?) {
            println("RESPONSE")

            title = response?.body()?.query?.results?.channel?.title
            println(response?.body()?.query?.results?.channel?.item?.forecast)
            var forecasts = response?.body()?.query?.results?.channel?.item?.forecast

            var forecastStrings = mutableListOf<String>()
            if (forecasts != null) {
                for (forecast in forecasts) {
                    val newString = "${forecast.date}\nHigh = ${forecast.high} Low = ${forecast.low}\n${forecast.text}"
                    forecastStrings.add(newString)
                }
            }

            var listView = findViewById<ListView>(R.id.forecastListView)
            var adapter = ArrayAdapter(this@ForecastActivity, android.R.layout.simple_list_item_1, forecastStrings)
            listView.adapter = adapter
        }


        override fun onFailure(call: Call<Weather>?, t: Throwable?) {
            println("FAILURE")
        }
    }

    val searchTerm = intent.extras.getString("searchTerm")

    retriever.getForecast(callback, searchTerm)
}

}

So I tried to make a list of “forecast” objects, but as you can see it, this does not contains any json array list but keys as “0” and “1” (there is no ‘[’)

I am sure the problem is not the API, as my friend is making the same app but for IOS, and this is working for him.

I suppose that, when more orders will be added, we should have a “4”, “5”, etc keys and values.

So my question is : How do I create a list (who will be a xml ListView, and will be automatically refreshed) from these kind of objects ?

(Sorry for posting so much code, but I am really stuck on that)
(Sorry for the bad indent, I can’t create a code snippet)

Thank you in advance.