diff --git a/app/src/main/java/com/livinglifetechway/k4kotlinsample/ApiInterface.kt b/app/src/main/java/com/livinglifetechway/k4kotlinsample/ApiInterface.kt new file mode 100644 index 0000000..10732e1 --- /dev/null +++ b/app/src/main/java/com/livinglifetechway/k4kotlinsample/ApiInterface.kt @@ -0,0 +1,41 @@ +package com.livinglifetechway.k4kotlinsample.RetrofitApi + +import retrofit2.Call +import retrofit2.http.GET + +/** + */ +interface ApiInterface { + @GET("posts/1") + fun getPost(): Call + + @GET("users") + fun getUserDetails(): Call + + @GET("aaaa/bbbb") + fun getUserDetailsError(): Call + +} + + +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 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 +) \ No newline at end of file diff --git a/app/src/main/java/com/livinglifetechway/k4kotlinsample/RetrofitActivity.kt b/app/src/main/java/com/livinglifetechway/k4kotlinsample/RetrofitActivity.kt index 431b0e7..23ffdbb 100644 --- a/app/src/main/java/com/livinglifetechway/k4kotlinsample/RetrofitActivity.kt +++ b/app/src/main/java/com/livinglifetechway/k4kotlinsample/RetrofitActivity.kt @@ -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() { @@ -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 = "" @@ -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") } @@ -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() diff --git a/app/src/main/res/layout/activity_retrofit.xml b/app/src/main/res/layout/activity_retrofit.xml index 5ec4bb8..06abfae 100644 --- a/app/src/main/res/layout/activity_retrofit.xml +++ b/app/src/main/res/layout/activity_retrofit.xml @@ -60,12 +60,6 @@ android:layout_height="wrap_content" android:text="Enqueue Await With Error" /> -