java - Returning Boolean value from Asynctask in Android -


i new android. want pass url asynctask , check if return status code sc_ok or not. method should return true if status code sc_ok returned , false if other status code.

class checking extends asynctask<string, void, boolean> {      @override     protected boolean doinbackground(string... params) {         // todo auto-generated method stub         boolean a;         httpclient client = new defaulthttpclient();         httpget httpget = new httpget(l1);               httpresponse response;         try {             response = client.execute(httpget);             statusline statusline = response.getstatusline();             int statuscode = statusline.getstatuscode();             if (statuscode != httpstatus.sc_ok) {                                                    toast.maketext(getapplicationcontext(),"no", toast.length_long).show();                 a= false;             }             else {                 toast.maketext(getapplicationcontext(),"yes", toast.length_long).show();                 a= true;              }         } catch (clientprotocolexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         finally{             return false;         }                   }      @override     protected void onpostexecute(boolean result) {      } } 

change whole thing (blind coding):

class checking extends asynctask<string, void, boolean> {     @override     protected boolean doinbackground(string... params) {         // todo auto-generated method stub         boolean a;         httpclient client = new defaulthttpclient();         httpget httpget = new httpget(l1);         httpresponse response;         try {             response = client.execute(httpget);             statusline statusline = response.getstatusline();             int statuscode = statusline.getstatuscode();             if (statuscode != httpstatus.sc_ok) {                 = false;             } else {                 = true;             }             return a;         } catch (clientprotocolexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         return false;     }     @override     protected void onpostexecute(boolean result) {         toast.maketext(getapplicationcontext(), (result ? "yes" : "no"), toast.length_long).show();     } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -