How I can get "error" or "success" from json in kotlin?

452 Views Asked by At

I try to make web android app in kotlin and everyday know something but about this error I didn't find solution. I work with OkHTTP3 and for Json I found Klaxon. How I can get from response "success" or "error" only. For example: if I get:

{
"error":{
"code":4,
"text":"\u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u043b\u043e\u0433\u0438\u043d \u0438\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u044c!"
}
}

I need to check is it "error" or "success". I tried ```if (response.header("error") toast ("I got error") or

if (response.header("success") {
val i = Intent(this, SecondActivity::class.java).putExtra("token", response.header("session_token").toString)
startActivity(i)

Do I need if ()? Maybe I can check success other way? Thank for all issues and maybe you recommend other library for json?

1

There are 1 best solutions below

0
On

I think that I found solution. I think it's good and work as must.

client.newCall(request).enqueue(object : Callback{

            override fun onResponse(call: Call, response: Response) {

                startActivity(Intent(this@Login, NavigationMenu::class.java)
                    .putExtra("session_token", response
                        .header("session_token")
                        .toString()))
                toast(response.header("text").toString())
            }