the code have below simple android login app built , want direct user page if login correct, user can login , @ profile, they're both directed same profile regardless of are. have 2 profile activity's niallprofile.xml , alannahprofile.xml, @ present both directed niallprofile want change if credentials 'alannah' , password entered direct page. can show me how this? preferably using intent if possible know how use them. code below checks see if login successful , if was, they're brought nialls profile
public class backgroundworker extends asynctask<string, void, string> { context context; alertdialog alertdialog; backgroundworker (context ctx){ context = ctx; } @override protected string doinbackground(string... params) { string type = params[0]; string login_url = ""; if (type.equals("login")) { try { string username = params[1]; string password = params[2]; url url = new url(login_url); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestmethod("post"); httpurlconnection.setdooutput(true); httpurlconnection.setdoinput(true); outputstream outputstream = httpurlconnection.getoutputstream(); bufferedwriter bufferedwriter = new bufferedwriter(new outputstreamwriter(outputstream, "utf-8")); string post_data = urlencoder.encode("username", "utf-8") + "=" + urlencoder.encode(username, "utf-8") + "&" + urlencoder.encode("password", "utf-8") + "=" + urlencoder.encode(password, "utf-8"); bufferedwriter.write(post_data); bufferedwriter.flush(); bufferedwriter.close(); outputstream.close(); inputstream inputstream = httpurlconnection.getinputstream(); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream, "iso-8859-1")); string result = ""; string line = ""; while ((line = bufferedreader.readline()) != null) { result += line; } bufferedreader.close(); inputstream.close(); httpurlconnection.disconnect(); return result; } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } @override protected void onpostexecute(string result) { if (result.contains("successful login")){ intent profileintent = new intent(context, niallprofile.class); context.startactivity(profileintent); } else { alertdialog.setmessage(result); alertdialog.show(); } }
in part check if ( username == "alannah")
store variable somewhere can access in onpostexecute
, or better make onpostexecute
accept additional parameter, onpostexecute(string username, string results)
and make check:
if (result.contains("successful login")){ if(username == "alannah") { intent profileintent = new intent(context, alannahprofile.class); context.startactivity(profileintent); } else { intent profileintent = new intent(context, niallprofile.class); context.startactivity(profileintent); } } else { alertdialog.setmessage(result); alertdialog.show(); }
if later support more users, consider using switch
statement instead of if-else
.
Comments
Post a Comment