Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 實現簡單登錄

android 實現簡單登錄

編輯:關於Android編程

一直知道WEB登錄之後是通過session保持回話的,現在安卓卻不知道怎麼弄了。

研究了一下,記錄一下。直接上代碼:

服務器端:

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"%>
<%
	//測試,只用一個用戶名登錄,不設密碼
	String userName = request.getParameter("userName");

	PrintWriter pw = response.getWriter();
	//只要用戶名符合集可登錄
	if(userName.equals("miquan")) {
		//返回安卓端的json
		JsonObject json = new JsonObject();
		json.addProperty("success", true);
		json.addProperty("sessionId", request.getSession().getId());
		
		//保存在服務器端的session
		request.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();
	//獲取到同一個session,而不是從新生成一個session,這樣就達到我要的效果了
	pw.write((String)session.getAttribute("userName"));
	pw.flush();
%>

安卓端:

			//登錄
			btnLogin.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					FinalHttp fh = new FinalHttp();
					fh.get("http://172.27.35.1:8080/login/index.jsp?userName=miquan", new AjaxCallBack() {
						@Override
						public void onSuccess(Object t) {
							//獲取返回來的json
							String str = t.toString();
							str = str.trim();
							try {
								JSONObject obj = new JSONObject(str);
								boolean success = obj.getBoolean("success");
								//登錄成功
								if(success) {
									//app = (MyApplication) this.getApplication();
									//MyApplication添加了屬性sessionId和isLogin
									app.setLogin(true);
									app.setSessionId(obj.getString("sessionId"));
									Toast.makeText(app, "登錄成功", Toast.LENGTH_SHORT).show();
								} else {
									Toast.makeText(app, "登錄失敗", Toast.LENGTH_SHORT).show();
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}
							super.onSuccess(t);
						}
						@Override
						public void onFailure(Throwable t, int errorNo,
								String strMsg) {
							Log.e("miquan", "failure  " + strMsg);
							super.onFailure(t, errorNo, strMsg);
						}
					});
				}
			});
			//測試
			btnSession.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					FinalHttp fh = new FinalHttp();
					//添加session,連請求一起發送,這裡服務器用的是java開發的
					fh.addHeader("Cookie", "JSESSIONID=" + app.getSessionId());
					fh.get("http://172.27.35.1:8080/login/session.jsp", new AjaxCallBack() {
						@Override
						public void onSuccess(Object t) {
							Log.e("miquan", t.toString());
							super.onSuccess(t);
						}
						@Override
						public void onFailure(Throwable t, int errorNo,
								String strMsg) {
							Log.e("miquan", "failure  " + strMsg);
							super.onFailure(t, errorNo, strMsg);
						}
					});
				}
			});
馬上下班了,不多寫。。。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved