Always know the web through the login session to answer, now, Android do not know how to do.
Studied a bit, it records. Directly in the code:
Server End:
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, only with a username login. Do not set passwordstring UserName = Request.getparameter ("UserName"); PrintWriter pw = Response.getwriter ();//Only userName conforming set can log in if (Username.equals ("Miquan")) {//Android JSON returned Jsonjsonobject = new Jsonobject (); Json.addproperty ("Success", true); Json.addproperty ("SessionId", Request.getsession (). GetId ()); /saved on server side of 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. That's what 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) {//get returned jsonstring str = t . toString (); str = Str.trim (); try {jsonobject obj = new Jsonobject (str); Boolean success = Obj.getboolean ("Success");// Login succeeded if (success) {//app = (MyApplication) this.getapplication ();//myapplication added attribute 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 ();//Join the session. Send the request together. Here the server is using 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);});});
Now work, write more ...
Copyright notice: This article blog original article, reproduced please specify the original address.
Android Simple Login