【ANDROID STUDIO】 Query Parameter Retrofit With Kotlin Coroutines

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.liveData
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Response

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val retService = RetrofitInstance
.getRetrofitInstance()
.create(AlbumService::class.java)
val responseLiveData: LiveData<Response<Albums>> = liveData {
//val response = retService.getAlbums()
val response = retService.getSortedAlbums(3)

emit(response)
}
responseLiveData.observe(this, Observer {
val albumsList = it.body()?.listIterator()
if (albumsList != null) {
while (albumsList.hasNext()) {
val albumsItem = albumsList.next()
val result = " " + "Album Title : ${albumsItem.title}" + "\n" +
" " + "Album id : ${albumsItem.id}" + "\n" +
" " + "User id : ${albumsItem.userId}" + "\n\n\n"
text_view.append(result)
}
}
})

}
}

package com.example.myapplication

import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query

interface AlbumService {
@GET("/albums")
suspend fun getAlbums() : Response<Albums>
@GET("/albums")
suspend fun getSortedAlbums(@Query("userId") userId:Int) :Response<Albums>

}

No comments:

My New Favorite Music Genre Is Polish Noir

These 10 artists create music that is mystical, melancholy, magical, and more. ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏    ...

Contact Form

Name

Email *

Message *