Android for simple Login

Source: Internet
Author: User

Always know that the Web login after the session to stay in reply, and now Android do not know how to do it.

Study it and record it for a moment. Directly on the code:

Server-side:

index.jsp

<% @page import= "Java.io.PrintWriter"%><% @page import= "Com.sun.org.apache.xml.internal.serialize.Printer "%><% @page import=" com.google.gson.JsonObject "%><%@ page language=" java "contenttype=" text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><%//test, log in with only one user name, no password string userName = Request.getparameter (" UserName "); PrintWriter pw = Response.getwriter ();//As long as the user name conforms to the set can log on if (Username.equals ("Miquan")) {//return android Jsonjsonobject JSON = new Jsonobject (); Json.addproperty ("Success", true); Json.addproperty ("SessionId", Request.getsession (). GetId ());// Saved on the server side of the Sessionrequest.getsession (). SetAttribute ("UserName", "I am a test name."); Pw.write (Json.tostring ());p W.flush ();} else {Jsonobject obj = new Jsonobject (); Obj.addproperty ("Success", false);p W.write (obj.tostring ());p W.flush ();} %>

session.jsp

<% @page import= "javax.websocket.Session"%><% @page import= "java.io.PrintWriter"%><%@ page language= " Java "contenttype=" text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><%printwriter pw = Response.getwriter ();//Get to the same session, Instead of starting a new session, it's going to get the effect I want. Pw.write (String) session.getattribute ("UserName"));p W.flush ();%>

android:

Login Btnlogin.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {finalhttp fh = new Finalhttp (); Fh.get ("Http://172.27.35.1:8080/login/index.jsp?userName=miquan", new ajaxcallback<object> () {@ overridepublic void onsuccess (Object t) {//Gets the returned jsonstring str = t.tostring (); str = Str.trim (); try {jsonobject obj = new Jsonobject (str); Boolean success = Obj.getboolean ("Success");//Login successful if (success) {//app = (MyApplication) This.getapplication ();//myapplication added Properties SessionID and Isloginapp.setlogin (true); App.setsessionid (Obj.getstring (" SessionId ")); Toast.maketext (App, "Login successful", Toast.length_short). Show (); else {toast.maketext (app, "Login Failed", Toast.length_short). Show ();}} catch (Jsonexception e) {e.printstacktrace ();} Super.onsuccess (t);} @Overridepublic void OnFailure (Throwable t, int errorno,string strMsg) {log.e ("Miquan", "failure" + STRMSG); super.onfail Ure (t, Errorno, STRMSG);});});

Test Btnsession.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {finalhttp fh = new Finalhttp ();//Add session, even request to send together, here server is Java developed Fh.addheader ("Cookie", "jsessionid=" + App.getsessionid ()); Fh.get ( "Http://172.27.35.1:8080/login/session.jsp", new Ajaxcallback<object> () {@Overridepublic void onsuccess ( Object t) {log.e ("Miquan", t.tostring ()); super.onsuccess (t);} @Overridepublic void OnFailure (Throwable t, int errorno,string strMsg) {log.e ("Miquan", "failure"  + STRMSG); Super.onfailure (t, Errorno, STRMSG);});});

Get off work now, don't write more ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.