Dheeraj Singh Bhadoria
09/04/2022, 10:10 AMsuspend fun getTopKeys(con: Context, rootObject: JSONObject): MutableLiveData<AuthResponse> {
return withContext(<http://Dispatchers.IO|Dispatchers.IO>){
val gson = Gson()
val accessToken = MyPrefConstant.getAccessToken(con)
val retrofit = RetrofitConfig.getRetrofitWithAccessToken(accessToken, con)
val service = retrofit?.create(ApiInterface::class.java)
var data = AesKotlin.encrypt(rootObject!!.toString(),AppConfig.ENCRYPTION_KEY)
Log.e("App1>>>>", data.replace("[\n]", "")!!.toString())
val call = service?.getTopKeys(data.replace("[\n]", "")!!.toString())
call?.enqueue(object : Callback<AuthResponse> {
override fun onResponse(call: Call<AuthResponse>, response: Response<AuthResponse>) {
if (response.code() == 200 && response.body().status.equals("OK")) {
val responseData = response.body()!!
searchData.value = gson.fromJson(gson.toJson(responseData), AuthResponse::class.java)
val i = Intent("return.apiResponse")
i.putExtra("data", gson.toJson(responseData).toString())
i.putExtra("method", "getTopN")
//con.sendBroadcast(i)
LocalBroadcastManager.getInstance(con).sendBroadcast(i)
}else{
// Toast.makeText(con, response.body().errorMessage, Toast.LENGTH_LONG).show()
val json = "{\"Status\": \"NOT OK\"," +
" \"ErrorMessage\": \"Something went wrong\" ," +
" \"Data\": \"NULL\", " +
" \"Timestamp\": \"NULL\" }"
searchData.value = gson.fromJson(json, AuthResponse::class.java)
}
}
override fun onFailure(call: Call<AuthResponse>, t: Throwable) {
Log.e("API","Error")
val json = "{\"Status\": \"NOT OK\"," +
" \"ErrorMessage\": \"Something went wrong \"," +
" \"Data\": \"NULL\", " +
" \"Timestamp\": \"NULL\" }"
searchData.value = gson.fromJson(json, AuthResponse::class.java)
}
})
searchData
}
}
In above method getTopKeys following methods are showing in mainthread in profiler
getJSON() - Related to gson
setValue - setting value to livedata
fromJSON - Related to gson
Joffrey
09/04/2022, 10:32 AMsuspendCancellableCoroutine
rishabhsinghbisht
09/04/2022, 12:50 PMcallbackExecutor