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 ...