package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
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() {
private lateinit var retService: AlbumService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
retService = RetrofitInstance
.getRetrofitInstance()
.create(AlbumService::class.java)
//getRequestWithQueryParameters()
//getRequestWithPathParameters()
uploadAlbum()
}
private fun uploadAlbum() {
val album = AlbumsItem(0, "My title", 3)
val postResponse: LiveData<Response<AlbumsItem>> = liveData {
val response = retService.uploadAlbum(album)
emit(response)
}
postResponse.observe(this, Observer {
val receivedAlbumsItem = it.body()
val result = " " + "Album Title : ${receivedAlbumsItem?.title}" + "\n" +
" " + "Album id : ${receivedAlbumsItem?.id}" + "\n" +
" " + "User id : ${receivedAlbumsItem?.userId}" + "\n\n\n"
text_view.text = result
})
}
private fun getRequestWithQueryParameters(){
val responseLiveData: LiveData<Response<Albums>> = liveData {
//val response = retService.getAlbums()
var 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)
}
}
})
}
private fun getRequestWithPathParameters(){
// path parameter example
val pathResponse : LiveData<Response<AlbumsItem>> = liveData {
val response = retService.getAlbum(3)
emit(response)
}
pathResponse.observe(this, Observer {
val title = it.body()?.title
Toast.makeText(applicationContext, title, Toast.LENGTH_LONG).show()
})
}
}
package com.example.myapplication
import retrofit2.Response
import retrofit2.http.*
interface AlbumService {
@GET("/albums")
suspend fun getAlbums() : Response<Albums>
@GET("/albums")
suspend fun getSortedAlbums(@Query("userId") userId:Int) :Response<Albums>
@GET("/albums/{id}")
suspend fun getAlbum(@Path(value = "id")albumId:Int) : Response<AlbumsItem>
@POST("/albums")
suspend fun uploadAlbum(@Body album: AlbumsItem): Response<AlbumsItem>
}