Skip to content

Commit

Permalink
Updated Retrofit example and removed unused button
Browse files Browse the repository at this point in the history
  • Loading branch information
kirtan403 committed Dec 28, 2017
1 parent c8dc215 commit 5930981
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.livinglifetechway.k4kotlinsample.RetrofitApi

import retrofit2.Call
import retrofit2.http.GET

/**
*/
interface ApiInterface {
@GET("posts/1")
fun getPost(): Call<Post>

@GET("users")
fun getUserDetails(): Call<UserResp>

@GET("aaaa/bbbb")
fun getUserDetailsError(): Call<UserResp>

}


data class Post(
val userId: Int, //1
val id: Int, //1
val title: String, //sunt aut facere repellat provident occaecati excepturi optio reprehenderit
val body: String //quia et suscipit suscipit recusandae consequuntur expedita et cum reprehenderit molestiae ut ut quas totam nostrum rerum est autem sunt rem eveniet architecto
)

data class UserResp(
val page: Int, //2
val per_page: Int, //3
val total: Int, //12
val total_pages: Int, //4
val data: List<Data>
)

data class Data(
val id: Int, //4
val first_name: String, //Eve
val last_name: String, //Holt
val avatar: String //https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import com.livinglifetechway.k4kotlinsample.RetrofitApi.ApiClient
import com.livinglifetechway.k4kotlinsample.databinding.ActivityRetrofitBinding
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.runBlocking

class RetrofitActivity : AppCompatActivity() {

Expand All @@ -20,21 +18,7 @@ class RetrofitActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
mBinding = setBindingView(R.layout.activity_retrofit)

// runBlocking {
// val job = launch(UI) {
// val enqueueAwait = ApiClient.service.getUserDetails().enqueueAwait(this@RetrofitActivity, RetrofitCallback {
// progressView = mBinding.progressBar
//
// on200Ok { call, response ->
// get original response object
// }
// })
// mBinding.tvResponse.text = enqueueAwait.toString()
// }
// }
// mBinding.tvInfo.text = "This is runblocking"


// simple enqueue with lifecycle owner
mBinding.btnEnqueue.setOnClickListener {
mBinding.tvResponse.text = ""
mBinding.tvInfo.text = ""
Expand All @@ -45,6 +29,9 @@ class RetrofitActivity : AppCompatActivity() {
mBinding.tvInfo.append("Response Received \n")
mBinding.tvResponse.text = response?.body()?.toString()
}
onFailureNotCancelled { call, throwable ->
mBinding.tvInfo.append("Error: ${throwable?.message}\n")
}
})
mBinding.tvInfo.append("API call enqueued \n")
}
Expand Down Expand Up @@ -99,30 +86,6 @@ class RetrofitActivity : AppCompatActivity() {
}
}



mBinding.btnEnqueueAwaitRunblocking.setOnClickListener {
mBinding.progressBar.show()
mBinding.tvResponse.text = ""
mBinding.tvInfo.text = ""

runBlocking {
launch(UI) {
mBinding.tvInfo.append("Starting API call \n")
val enqueueAwait = ApiClient.service.getUserDetails().enqueueAwait(this@RetrofitActivity, RetrofitCallback {
progressView = mBinding.progressBar

on200Ok { call, response ->
mBinding.tvInfo.append("Response received \n")
}
})
mBinding.tvInfo.append("Await call completed \n")
mBinding.tvResponse.text = enqueueAwait.toString()
}
}
mBinding.tvInfo.append("Run Blocking completed \n")
}
//
// deferred response body
mBinding.btnEnqueueDeferred.setOnClickListener {
mBinding.progressBar.show()
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/res/layout/activity_retrofit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@
android:layout_height="wrap_content"
android:text="Enqueue Await With Error" />

<Button
android:id="@+id/btn_enqueue_await_runblocking"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enqueue Await in Runblocking" />

<Button
android:id="@+id/btn_enqueue_deferred"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ fun <T> Call<T>.enqueue(lifecycleOwner: LifecycleOwner, callback: Callback<T>) {
* You can optionally pass the lifecycleOwner (activity/fragment) to scope the call.
* Also you can optionally pass the callback function as well,
* it will be called before returning results.
*
* NOTE: This is an experimental API. Please report bugs if you find any.
*/
suspend fun <T> Call<T>.enqueueAwait(lifeCycleOwner: LifecycleOwner? = null, callback: Callback<T>? = null): T {

Expand Down Expand Up @@ -82,6 +84,8 @@ suspend fun <T> Call<T>.enqueueAwait(lifeCycleOwner: LifecycleOwner? = null, cal
* You can optionally pass the lifecycleOwner (activity/fragment) to scope the call.
* Also you can optionally pass the callback function as well,
* it will be called when results are available.
*
* NOTE: This is an experimental API. Please report bugs if you find any.
*/
fun <T> Call<T>.enqueueDeferred(lifeCycleOwner: LifecycleOwner? = null, callback: Callback<T>? = null): CompletableDeferred<T> {

Expand Down Expand Up @@ -125,6 +129,8 @@ fun <T> Call<T>.enqueueDeferred(lifeCycleOwner: LifecycleOwner? = null, callback
* You can optionally pass the lifecycleOwner (activity/fragment) to scope the call.
* Also you can optionally pass the callback function as well,
* it will be called when results are available.
*
* NOTE: This is an experimental API. Please report bugs if you find any.
*/
fun <T> Call<T>.enqueueDeferredResponse(lifeCycleOwner: LifecycleOwner? = null, callback: Callback<T>? = null): CompletableDeferred<Response<T>> {

Expand Down

0 comments on commit 5930981

Please sign in to comment.