Request data from API

Hi, I would like to request top game data for my test application https://dev.twitch.tv/docs/v5/reference/games, but I have difficulties at the data request stage. Tell me what I should correct please

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
            val o =
                createRequest("https://api.twitch.tv/kraken/games/top")
                    .map { Gson().fromJson(it, Feed::class.java) }
                    .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
    }
}

class Feed(
    val items: ArrayList<FeedItem>
)
class FeedItem(
    val name: String, val channels: String, val viewers: String, val logo: ImageView
)
//CreateRequest func in utils:
fun createRequest (url:String) = Observable.create<String> {
val urlConnection = URL (url).openConnection() as HttpURLConnection
urlConnection.requestMethod = "GET"
urlConnection.setRequestProperty("Client-ID",
    "0mbng65k95r72we0j1ii1uf02cssrs")
try {
    urlConnection.connect()

    if(urlConnection.responseCode != HttpURLConnection.HTTP_OK)
        it.onError(RuntimeException(urlConnection.responseMessage))
    else {
        val str = urlConnection.inputStream.bufferedReader().readText()
        it.onNext(str)
    }
} finally {
    urlConnection.disconnect()
}
}

What is the error(s) you are getting?

I feel like I’m doing something wrong. I dont know how get Response like this:
{
“_total”: 1157,
“top”: [
{
“channels”: 953,
“viewers”: 171708,
“game”: {
“_id”: 32399,
“box”: {
“large”: “https://static-cdn.jtvnw.net/ttv-boxart/Counter-Strike:%20Global%20Offensive-272x380.jpg”,
“medium”: “https://static-cdn.jtvnw.net/ttv-boxart/Counter-Strike:%20Global%20Offensive-136x190.jpg”,
“small”: “https://static-cdn.jtvnw.net/ttv-boxart/Counter-Strike:%20Global%20Offensive-52x72.jpg”,
“template”: “https://static-cdn.jtvnw.net/ttv-boxart/Counter-Strike:%20Global%20Offensive-{width}x{height}.jpg
},
“giantbomb_id”: 36113,
“logo”: {
“large”: “https://static-cdn.jtvnw.net/ttv-logoart/Counter-Strike:%20Global%20Offensive-240x144.jpg”,
“medium”: “https://static-cdn.jtvnw.net/ttv-logoart/Counter-Strike:%20Global%20Offensive-120x72.jpg”,
“small”: “https://static-cdn.jtvnw.net/ttv-logoart/Counter-Strike:%20Global%20Offensive-60x36.jpg”,
“template”: “https://static-cdn.jtvnw.net/ttv-logoart/Counter-Strike:%20Global%20Offensive-{width}x{height}.jpg
},
“name”: “Counter-Strike: Global Offensive”,
“popularity”: 170487
}
},

]
}

So you are saying you code doesn’t return anything at all, or the response you get is what you have pasted?

You don’t appear to be sending the Accept: application/vnd.twitchtv.v5+json header, so are likely getting a 410 response because you’re sending a request to a version of the API (v3) that doesn’t exist any more.

I think you’re right. Could you show how to add a missing item?

The same way you added the client ID

urlConnection.setRequestProperty(“Accept”, “application/vnd.twitchtv.v5+json”)

thanks a lot!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.