Skip to content

Instantly share code, notes, and snippets.

@humayuntanwar
Last active July 5, 2019 06:39
Show Gist options
  • Save humayuntanwar/12b9ac6f8b8c52587418388d4aaa1083 to your computer and use it in GitHub Desktop.
Save humayuntanwar/12b9ac6f8b8c52587418388d4aaa1083 to your computer and use it in GitHub Desktop.
Custom Retrofit CallBack Class to handle Custom Responses.
import android.app.Dialog
import android.content.Context
import android.util.Log
import android.view.View
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
open class RetrofitCallback(private val mContext: Context) : Callback<GeneralResponse> {
private lateinit var progressDialog: Dialog
init {
progressDialog = Util.ProgressDialog(mContext)
progressDialog.show()
}
override fun onResponse(call: Call<GeneralResponse>, response: Response<GeneralResponse>) {
if (response.body() == null) {
//response is null
} else {
if (response.body()!!.code=="3333") {
//session timed out }
else if (response.body()!!.code=="1111") {
//failed }
}
}
override fun onFailure(call: Call<GeneralResponse>, t: Throwable) {
hideProgress()
if (t.message != null) {
Log.e(TAG, t.message)
}
}
private fun hideProgress() {
progressDialog.dismiss()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment