java - Android Static functions -
i'm wondering how can access return statement static function. have static function async , want return statement in class - know sounds complex but, i'm sure it's easy solution.
login.class
public class login extends activity { button login; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.login); textview top = (textview) findviewbyid(r.id.textview2); final edittext user = (edittext) findviewbyid(r.id.etuser); final edittext pass = (edittext) findviewbyid(r.id.etpass); checkbox stay = (checkbox) findviewbyid(r.id.cbstay); button login = (button) findviewbyid(r.id.btlogin); login.setonclicklistener( new view.onclicklistener() { public void onclick(view v) { // todo auto-generated method stub string user1 = user.gettext().tostring(); string pass1 = pass.gettext().tostring(); if(user1 !=null &user1.length()>=1 & pass1 !=null &pass1.length()>=1) { comhelper.sendlogin(user1, pass1); } } }); } } comhelper.class
public class comhelper extends asynctask<string, void, string> { static string adress ="http://gta5news.com/login.php"; string user; string pass; public static boolean sendlogin(string user1, string pass1){ string user = user1.tostring(); string pass = pass1.tostring(); new comhelper().execute(user1, pass1, adress); return true; } private static stringbuilder inputstreamtostring(inputstream is) { string line = ""; stringbuilder total = new stringbuilder(); // wrap bufferedreader around inputstream bufferedreader rd = new bufferedreader(new inputstreamreader(is)); // read response until end try { while ((line = rd.readline()) != null) { total.append(line); } } catch (ioexception e) { e.printstacktrace(); } // return full string return total; } @override protected string doinbackground(string... params) { // todo auto-generated method stub inputstream inputstream = null; httpclient httpclient = new defaulthttpclient(); httppost post = new httppost(adress); try { /*add data namevaluepairs */ list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("user", user)); namevaluepairs.add(new basicnamevaluepair("password", pass)); post.setentity(new urlencodedformentity(namevaluepairs)); /*execute */ httpresponse response = httpclient.execute(post); string str = inputstreamtostring(response.getentity().getcontent()) .tostring(); log.w("httppost", str); if (str.tostring().equalsignorecase("true")) return str; } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } { } return null; } } now, want see if comhelper.sendlogin() returned true/or @ least returned something.
edit: when code executed nothing happens, guess that's because i'm not doing return statement.
if want @ value, need save return value of method in local variable
if(user1 !=null && user1.length() > 0 && pass1 !=null && pass1.length() > 0) { boolean comlogin = comhelper.sendlogin(user1, pass1); if(comlogin) { //do } }
Comments
Post a Comment