java - returning one between three int possibilities after Json -


i doing android login call api, response 2 different json:

a.- json error {"message":" not found","error":"unauthorized","status":401}

b.-json success {"permissions":10,"ac_status":"active"}

my idea return main screen 1 of 3 (int) values after check json tags:

return 0 if error getting json (like server down)
return 1 if login success
return 2 if error in login (sever error , json null)

i trying on asynctask,it works 0 on server error , login success. how can it? in advanced.

@override  protected void doinbackground(void... params) { ... ...  int valuereturn =0; jsonobject response = jsonservice.call(url);   try {    if (response.getstring("tag_error")      valuereturn = 2;    else      valuereturn= 1;     } catch (jsonexception e) {      valuereturn = 0;    } 

works 0 on server error , login success.

because tag_error key not exists in provided json strings control going in catch block.

 how can it? 

use jsonobject.has(string key_name) check if key exists or not before accessing value as:

if (response.has("error"){     // server error }else if (response.has("ac_status"){       // login success }else{  //error getting json  } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -