0

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?

savera sleemy
  • 73
  • 1
  • 12
  • 1
    Why don't you use OkHttp onresponse and onerror methods? you may need to modify the api. please check here https://stackoverflow.com/questions/24246783/okhttp-response-callbacks-on-the-main-thread – Emre Akcan Dec 10 '19 at 06:14
  • @EmreAkcanI I can code `val i = Intent(this, SomeActivity::class.java).putExtra("token", response.header("session_token").toString startACtivity(i)` in onResponse? And it will work how if i got success? – savera sleemy Dec 10 '19 at 07:01

1 Answers1

0

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())
            }
savera sleemy
  • 73
  • 1
  • 12