Simple login for android

Source: Internet
Author: User

Simple login for android

After logging on to the WEB, you always know that session is used to keep the session. Now Android doesn't know how to get it done.

After studying it, record it. Directly run the Code:

Server:

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 On with only one user name without a password String userName = request. getParameter ("userName"); PrintWriter pw = response. getWriter (); // you can log on to if (userName. equals ("miquan") {// return the android jsonJsonObject json = new JsonObject (); json. addProperty ("success", true); json. addProperty ("sessionId", request. getSession (). getId (); // Save the sessionrequest on the server side. getSession (). setAttribute ("userName", "I am a test name. "); pw. write (json. toString (); pw. flush ();} else {JsonObject obj = new JsonObject (); obj. addProperty ("success", false); pw. write (obj. toString (); pw. 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 the same session, instead of generating a new session, which achieves the expected effect pw. write (String) session. getAttribute ("userName"); pw. flush (); %>

Android:

// Log on to 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() {@ Overridepublic void onSuccess (Object t) {// get 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 (); // The attributes sessionId and isLoginapp are added to MyApplication. setLogin (true); app. setSessionId (obj. getString ("sessionId"); Toast. makeText (app, "Logon successful", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (app, "Logon 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. onFailure (t, errorNo, strMsg );}});}});
// Test btnSession. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {FinalHttp fh = new FinalHttp (); // Add a session and send the session together with the request, here, the server uses java-developed fh. addHeader ("Cookie", "JSESSIONID =" + app. getSessionId (); fh. get ("http: // 172.27.35.1: 8080/login/session. jsp ", new AjaxCallBack() {@ 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 );}});}});
I am in and out of work. I don't need to write much...

Related Article

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.