Android Simple Login

Source: Internet
Author: User

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

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.