android获取web服务器端session并验证登陆

来源:互联网 发布:有限元分析软件pfepg 编辑:程序博客网 时间:2024/06/16 22:21

传统网页实现用户登陆一般采用session或cookie记录用户基本信息又或者两者结合起来使用。android也可以采用session实现用户登陆验证并记录用户登陆状态时的基本信息,session是在服务器端的;而类似cookie的记录方式,则可以在客户端采用xml文件记录用户基本信息,重要数据则可以加密存放客户端。android实现的session登陆功能与网页请求不同的是,网页形式的一次成功的登陆请求后,再点击其他页面时,session一直是存在的,在一定时间内是有效的;而采用android客户端请求的一次成功登陆后,再次发送新的请求,则会产生新的session,而不是原来的。这就需要记录session的id号,并在整个请求过程中都记录并传递这个id号,才能保证session的一致性。

以获取php session为例,主要思路实现分为客户端与服务器端3个步骤。


附件:GetWebSession.zip(71.44 KB, 下载次数: 2237)
1.)客户端(Android)

建立一个名为GetWebSession的android项目,编写GetWebSession.java,LoginSuccessActivity.java,GetUserInfoActivity.java三个activity类。

1. GetWebSession.java主要是实现布局界面以及发送用户名和密码到php服务器端验证,如果验证成功则跳转到LoginSuccessActivity.java类。GetWebSession.java主要涉及到与服务器端连接请求,对从服务器端返回的json数据(如用户id,session等)进行解析,并存入HashMap,传递到LoginSuccessActivity.java

代码如下:

  1. package com.login.main;
  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.apache.http.message.BasicNameValuePair;
  14. import org.apache.http.protocol.HTTP;
  15. import org.apache.http.util.EntityUtils;
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18. import android.app.Activity;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import android.os.Bundle;
  22. import android.view.View;
  23. import android.view.View.OnClickListener;
  24. import android.widget.Button;
  25. import android.widget.EditText;
  26. import android.widget.Toast;
  27. public class GetWebSession extends Activity {
  28. /** Called when the activity is first created. */
  29. private EditText user;
  30. private EditText password;
  31. private Button loginBtn;
  32. private Button logoutBtn;
  33. //主要是记录用户会话过程中的一些用户的基本信息
  34. private HashMap<String, String> session =new HashMap<String, String>();
  35. @Override
  36. public void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.main);
  39. user=(EditText)findViewById(R.id.user);
  40. password=(EditText)findViewById(R.id.password);
  41. loginBtn=(Button)findViewById(R.id.loginBtn);
  42. loginBtn.setOnClickListener(loginClick);
  43. logoutBtn=(Button)findViewById(R.id.logoutBtn);
  44. logoutBtn.setOnClickListener(logoutClick);
  45. }
  46. OnClickListener loginClick=new OnClickListener() {
  47. @Override
  48. public void onClick(View v) {
  49. // TODO Auto-generated method stub
  50. if(checkUser()){
  51. Toast.makeText(v.getContext(), "用户登录成功!", Toast.LENGTH_SHORT).show();
  52. Context context = v.getContext();
  53. Intent intent = new Intent(context,
  54. LoginSuccessActivity.class);
  55. //传递session参数,在用户登录成功后为session初始化赋值,即传递HashMap的值
  56. Bundle map = new Bundle();
  57. map.putSerializable("sessionid", session);
  58. intent.putExtra("session", map);
  59. context.startActivity(intent); // 跳转到成功页面
  60. }
  61. else
  62. Toast.makeText(v.getContext(), "用户验证失败!", Toast.LENGTH_SHORT).show();
  63. }
  64. };
  65. OnClickListener logoutClick=new OnClickListener() {
  66. @Override
  67. public void onClick(View v) {
  68. // TODO Auto-generated method stub
  69. System.exit(0);
  70. }
  71. };
  72. private boolean checkUser(){
  73. String username=user.getText().toString();
  74. String pass=password.getText().toString();
  75. DefaultHttpClient mHttpClient = new DefaultHttpClient();
  76. HttpPost mPost = new HttpPost("http://10.0.2.2/web/php/login.php");
  77. //传递用户名和密码相当于
  78. //http://10.0.2.2/web/php/login.php?username=''&password=''
  79. List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
  80. pairs.add(new BasicNameValuePair("username", username));
  81. pairs.add(new BasicNameValuePair("password", pass));
  82. try {
  83. mPost.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
  84. } catch (UnsupportedEncodingException e) {
  85. // TODO Auto-generated catch block
  86. e.printStackTrace();
  87. }
  88. try {
  89. HttpResponse response = mHttpClient.execute(mPost);
  90. int res = response.getStatusLine().getStatusCode();
  91. if (res == 200) {
  92. HttpEntity entity = response.getEntity();
  93. if (entity != null) {
  94. String info = EntityUtils.toString(entity);
  95. System.out.println("info-----------"+info);
  96. //以下主要是对服务器端返回的数据进行解析
  97. JSONObject jsonObject=null;
  98. //flag为登录成功与否的标记,从服务器端返回的数据
  99. String flag="";
  100. String name="";
  101. String userid="";
  102. String sessionid="";
  103. try {
  104. jsonObject = new JSONObject(info);
  105. flag = jsonObject.getString("flag");
  106. name = jsonObject.getString("name");
  107. userid = jsonObject.getString("userid");
  108. sessionid = jsonObject.getString("sessionid");
  109. } catch (JSONException e) {
  110. // TODO Auto-generated catch block
  111. e.printStackTrace();
  112. }
  113. //根据服务器端返回的标记,判断服务端端验证是否成功
  114. if(flag.equals("success")){
  115. //为session传递相应的值,用于在session过程中记录相关用户信息
  116. session.put("s_userid", userid);
  117. session.put("s_username", name);
  118. session.put("s_sessionid", sessionid);
  119. return true;
  120. }
  121. else{
  122. return false;
  123. }
  124. }
  125. else{
  126. return false;
  127. }
  128. }
  129. } catch (ClientProtocolException e) {
  130. // TODO Auto-generated catch block
  131. e.printStackTrace();
  132. } catch (IOException e) {
  133. // TODO Auto-generated catch block
  134. e.printStackTrace();
  135. }
  136. return false;
  137. }
  138. }

复制代码

2. LoginSuccessActivity.java主要获取php的session唯一的标识id以及用户的一些基本信息,session id则作为本次用户登录状态在服务器的唯一标识,即确定用户的唯一状态进行相关操作。LoginSuccessActivity.java类的方法与GetWebSession.java类似。其主要功能是获取session id后再次发送session id到服务器进行验证,根据封装的session数据验证用户操作权限等。

代码如下:

  1. package com.login.main;
  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.apache.http.message.BasicNameValuePair;
  14. import org.apache.http.protocol.HTTP;
  15. import org.apache.http.util.EntityUtils;
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18. import android.app.Activity;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import android.os.Bundle;
  22. import android.view.View;
  23. import android.view.View.OnClickListener;
  24. import android.widget.Button;
  25. import android.widget.TextView;
  26. import android.widget.Toast;
  27. public class LoginSuccessActivity extends Activity{
  28. private HashMap<String, String>session;
  29. @SuppressWarnings("unchecked")
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. // TODO Auto-generated method stub
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.login_success);
  35. //获取从登录成功后界面的传递的参数
  36. session =  (HashMap<String, String>) this.getIntent()
  37. .getBundleExtra("session").getSerializable("sessionid");
  38. //读取session的基本信息,并显示相应的控件
  39. String userid_info=session.get("s_userid");
  40. String username_info=session.get("s_username");
  41. String session_id=session.get("s_sessionid");
  42. //显示相应的内容到控件
  43. TextView userid_show=(TextView)findViewById(R.id.userid_show);
  44. userid_show.setText(userid_info);
  45. TextView username_show=(TextView)findViewById(R.id.username_show);
  46. username_show.setText(username_info);
  47. TextView sessionid_show=(TextView)findViewById(R.id.sessionid_show);
  48. sessionid_show.setText(session_id);
  49. //根据本次session再次获取用户信息
  50. Button getInfo=(Button)findViewById(R.id.getinfo);
  51. getInfo.setOnClickListener(getInfoClick);
  52. }
  53. OnClickListener getInfoClick=new OnClickListener() {
  54. @Override
  55. public void onClick(View v) {
  56. // TODO Auto-generated method stub
  57. if(getUserInfo()){
  58. Context context = v.getContext();
  59. Intent intent = new Intent(context,
  60. GetUserInfoActivity.class);
  61. //传递session参数,在用户登录成功后为session初始化赋值,即传递HashMap的值
  62. Bundle map = new Bundle();
  63. map.putSerializable("sessionid", session);
  64. intent.putExtra("session", map);
  65. context.startActivity(intent); // 跳转到成功页面
  66. }else{
  67. Toast.makeText(v.getContext(), "数据为空!", Toast.LENGTH_SHORT).show();
  68. }
  69. }
  70. };
  71. private boolean getUserInfo(){
  72. String sess_username=session.get("s_username");
  73. String sess_userid=session.get("s_userid");
  74. String sess_id=session.get("s_sessionid");
  75. DefaultHttpClient mHttpClient = new DefaultHttpClient();
  76. HttpPost mPost = new HttpPost("http://10.0.2.2/web/php/getinfo.php");
  77. List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
  78. pairs.add(new BasicNameValuePair("sess_userid", sess_userid));
  79. pairs.add(new BasicNameValuePair("sess_username", sess_username));
  80. pairs.add(new BasicNameValuePair("sess_sessionid", sess_id));
  81. try {
  82. mPost.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
  83. } catch (UnsupportedEncodingException e) {
  84. // TODO Auto-generated catch block
  85. e.printStackTrace();
  86. }
  87. try {
  88. HttpResponse response = mHttpClient.execute(mPost);
  89. int res = response.getStatusLine().getStatusCode();
  90. if (res == 200) {
  91. HttpEntity entity = response.getEntity();
  92. if (entity != null) {
  93. String info = EntityUtils.toString(entity);
  94. System.out.println("info-----------"+info);
  95. //以下主要是对服务器端返回的数据进行解析
  96. JSONObject jsonObject=null;
  97. //flag为登录成功与否的标记,从服务器端返回的数据
  98. String flag="";
  99. String userinfo="";
  100. String level="";
  101. String sessionid="";
  102. try {
  103. jsonObject = new JSONObject(info);
  104. flag = jsonObject.getString("flag");
  105. userinfo = jsonObject.getString("info");
  106. level = jsonObject.getString("level");
  107. sessionid = jsonObject.getString("sessionid");
  108. } catch (JSONException e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. }
  112. //根据服务器端返回的标记,判断服务端端验证是否成功
  113. if(flag.equals("notempty")){
  114. //为session传递相应的值,用于在session过程中记录相关用户信息
  115. session.put("info_userinfo", userinfo);
  116. session.put("info_level", level);
  117. session.put("info_sessionid", sessionid);
  118. return true;
  119. }
  120. else{
  121. return false;
  122. }
  123. }
  124. else{
  125. return false;
  126. }
  127. }
  128. } catch (ClientProtocolException e) {
  129. // TODO Auto-generated catch block
  130. e.printStackTrace();
  131. } catch (IOException e) {
  132. // TODO Auto-generated catch block
  133. e.printStackTrace();
  134. }
  135. return false;
  136. }
  137. }
复制代码

3.GetUserInfoActivity.java类是根据用户登录后产生唯一session 标识进行操作获取用户详细信息的类。

代码如下:

  1. package com.login.main;
  2. import java.util.HashMap;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. public class GetUserInfoActivity extends Activity{
  7. private HashMap<String, String>session;
  8. @SuppressWarnings("unchecked")
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. // TODO Auto-generated method stub
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.get_info);
  14. //获取从登录成功后界面的再次传递的参数
  15. session =  (HashMap<String, String>) this.getIntent().
  16. getBundleExtra("session").getSerializable("sessionid");
  17. //读取session的基本信息,并显示相应的控件
  18. String session_info=session.get("info_userinfo");
  19. String session_level=session.get("info_level");
  20. String session_id=session.get("info_sessionid");
  21. //显示相应的内容到控件
  22. System.out.println("session_info--------"+session_info);
  23. TextView get_info=(TextView)findViewById(R.id.get_info);
  24. get_info.setText(session_info);
  25. TextView get_level=(TextView)findViewById(R.id.get_level);
  26. get_level.setText(session_level);
  27. TextView get_sessionid=(TextView)findViewById(R.id.get_sessionid);
  28. get_sessionid.setText(session_id);
  29. }
  30. }
复制代码

4.三个布局的xml文件

(1.)main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent" >
  6. <TextView android:layout_height="wrap_content"
  7. android:layout_width="wrap_content"
  8. android:text="用户"></TextView>
  9. <EditText android:layout_height="wrap_content"
  10. android:text="" android:layout_width="fill_parent"
  11. android:singleLine="true" android:id="@+id/user"  ></EditText>
  12. <TextView android:layout_height="wrap_content"
  13. android:layout_width="wrap_content"
  14. android:text="密码"></TextView>
  15. <EditText android:id="@+id/password"
  16. android:layout_height="wrap_content"
  17. android:text="" android:layout_width="fill_parent"
  18. android:password="true" android:singleLine="true"></EditText>
  19. <LinearLayout android:layout_height="wrap_content"
  20. android:layout_width="fill_parent"
  21. android:orientation="horizontal"
  22. android:paddingLeft="0dip">

  23. <TableRow android:layout_width="fill_parent"
  24. android:layout_height="wrap_content">
  25. <Button android:layout_height="fill_parent"
  26. android:layout_width="fill_parent" android:text="登录"
  27. android:id="@+id/loginBtn"
  28. android:layout_weight="1"></Button>
  29. <Button android:layout_height="fill_parent"
  30. android:layout_width="fill_parent"
  31. android:text="退出"
  32. android:id="@+id/logoutBtn"
  33. android:layout_weight="1"></Button>

  34. </TableRow> </LinearLayout> </LinearLayout>

复制代码
(2.)login_success.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent" android:layout_height="fill_parent"
  4. android:orientation="vertical">
  5. <LinearLayout android:layout_height="wrap_content"
  6. android:layout_width="fill_parent"
  7. android:orientation="horizontal"
  8. android:paddingLeft="0dip">
  9. <TextView
  10. android:layout_height="fill_parent"
  11. android:layout_width="wrap_content"
  12. android:text="用户ID:"   >
  13. </TextView>
  14. <TextView android:layout_height="fill_parent"
  15. android:layout_width="fill_parent"
  16. android:text=""
  17. android:id="@+id/userid_show" ></TextView>
  18. </LinearLayout>
  19. <LinearLayout android:layout_height="wrap_content"
  20. android:layout_width="fill_parent"
  21. android:orientation="horizontal"
  22. android:paddingLeft="0dip">
  23. <TextView android:layout_height="fill_parent"
  24. android:layout_width="wrap_content"
  25. android:text="用户名: "   ></TextView>
  26. <TextView android:layout_height="fill_parent"
  27. android:layout_width="fill_parent"
  28. android:text=""
  29. android:id="@+id/username_show" ></TextView>
  30. </LinearLayout>
  31. <LinearLayout android:layout_height="wrap_content"
  32. android:layout_width="fill_parent"
  33. android:orientation="horizontal"
  34. android:paddingLeft="0dip">
  35. <TextView android:layout_height="fill_parent"
  36. android:layout_width="wrap_content"
  37. android:text="本次会话:"   ></TextView>
  38. <TextView android:layout_height="fill_parent"
  39. android:layout_width="fill_parent"
  40. android:text=""
  41. android:id="@+id/sessionid_show" ></TextView>
  42. </LinearLayout>
  43. <LinearLayout android:layout_height="wrap_content"
  44. android:layout_width="fill_parent"
  45. android:orientation="horizontal"
  46. android:paddingLeft="0dip">
  47. <Button android:layout_height="fill_parent"
  48. android:layout_width="wrap_content"
  49. android:id="@+id/getinfo"
  50. android:text="根据本次会话再次获取用户信息"  
  51. ></Button>
  52. </LinearLayout>
  53. </LinearLayout>
复制代码
(3.)get_info.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <LinearLayout android:layout_height="wrap_content"
  7. android:layout_width="fill_parent"
  8. android:orientation="horizontal"
  9. android:paddingLeft="0dip">
  10. <TextView android:layout_height="fill_parent"
  11. android:layout_width="wrap_content"
  12. android:text="用户信息: "   >
  13. </TextView>
  14. <TextView android:layout_height="fill_parent"
  15. android:layout_width="fill_parent"
  16. android:text=""
  17. android:id="@+id/get_info" ></TextView>
  18. </LinearLayout>
  19. <LinearLayout android:layout_height="wrap_content"
  20. android:layout_width="fill_parent"
  21. android:orientation="horizontal"
  22. android:paddingLeft="0dip">
  23. <TextView android:layout_height="fill_parent"
  24. android:layout_width="wrap_content"
  25. android:text="用户级别:"   ></TextView>
  26. <TextView android:layout_height="fill_parent"
  27. android:layout_width="fill_parent"
  28. android:text=""
  29. android:id="@+id/get_level" ></TextView>
  30. </LinearLayout>

  31. <LinearLayout android:layout_height="wrap_content"
  32. android:layout_width="fill_parent"
  33. android:orientation="horizontal"
  34. android:paddingLeft="0dip">
  35. <TextView android:layout_height="fill_parent"
  36. android:layout_width="wrap_content"
  37. android:text="本次会话:"   ></TextView>
  38. <TextView android:layout_height="fill_parent"
  39. android:layout_width="fill_parent" android:text=""
  40. android:id="@+id/get_sessionid" ></TextView>
  41. </LinearLayout>
  42. <LinearLayout android:layout_height="wrap_content"
  43. android:layout_width="fill_parent"
  44. android:orientation="horizontal"
  45. android:paddingLeft="0dip"> </LinearLayout> </LinearLayout>
复制代码
2.)服务器端(php)

php服务器端主要有三个文件,conn.php,login.php和getinfo.php。

1. conn.php是连接数据库的配置文件。

2. login.php主要是用来验证android客户端发送的请求,请求成功则返回flag=’success’的状态标识,采用数组记录用户基本信息,存储用户数据到session,并且记录本次产生的session id。用户基本数据及本次session产生的id均封装成json格式(json_encode($arr)),发送android客户端。产生本次session id的方法

$sessionid=session_id();//注意没有参数

具体代码如下:

  1. <?php
  2. header("Content-Type: text/html; charset=utf-8") ;
  3. //包含数据库连接文件
  4. include('conn.php');
  5. session_start();
  6. $username = htmlspecialchars($_POST["username"]);
  7. $password=$_POST["password"];
  8. mysql_query("set names utf8");
  9. //检测用户名及密码是否正确
  10. $check_query = mysql_query("select id ,name from user where name='$username' and
  11. password='$password' limit 1");
  12. $arr=array();//空的数组,该数组主要是格式化数据并封装成json格式发送到客户端
  13. if($result = mysql_fetch_array($check_query)){
  14. //登录成功
  15. $_SESSION['username'] = $result['name'];
  16. $_SESSION['userid'] = $result['id'];
  17. //获取当前session id
  18. $sessionid=session_id();
  19. $_SESSION['$sessionid'] = $sessionid;
  20. $arr = array(
  21. 'flag'=>'success',
  22. 'name'=>$result['name'],
  23. 'userid'=>$result['id'],
  24. 'sessionid'=>$sessionid
  25. );
  26. //封装json,如果php版本低于5.2,则不支持json_encode()方法,
  27. //可以参考本文件夹中php_json_encode.php中php_json_encode()方法代替json_encode();
  28. echo json_encode($arr);
  29. } else {
  30. $arr = array(
  31. 'flag'=>'error',
  32. 'name'=>'',
  33. 'userid'=>'',
  34. 'sessionid'=>''
  35. ); //封装json,如果php版本低于5.2,则不支持json_encode()方法,
  36. //可以参考本文件夹中php_json_encode.php中php_json_encode()方法代替json_encode();
  37. echo json_encode($arr);
  38. }
  39. ?>
复制代码

3. getinfo.php文件主要是用户再次查询信息验证session,而不是重新产生session,以记录用户状态。通过验证flag是否为empty判断数据是否显示。最后封装成json发送到客户端

获取本次session的方法:

$sessionid=$_POST["sess_sessionid"];//获取android客户端的sessionid

session_id($sessionid);//有参数

session_start();//启动session

具体代码如下:

  1. <?php
  2. header("Content-Type: text/html; charset=utf-8") ;
  3. include('conn.php');
  4. //获取从客户端LoginSuccessActivity类传递的参数
  5. $userid=$_POST["sess_userid"];
  6. $username=$_POST["sess_username"];
  7. //获取客户端传递的session标识
  8. $sessionid=$_POST["sess_sessionid"];
  9. session_id($sessionid);
  10. //将会根据session id获得原来的session
  11. session_start();
  12. //获取服务器端原来session记录的username,并且根据客户端传过来的username比较进行验证操作
  13. $sess_username=$_SESSION['username'];
  14. if($username==$sess_username){
  15. mysql_query("set names utf8");
  16. //查询用户基本信息
  17. $check_query = mysql_query("select userinfo,level from info where userid='$userid'  limit 1");
  18. $arr=array();//空的数组
  19. if($result = mysql_fetch_array($check_query)){
  20. $arr = array(
  21. 'flag'=>'notempty',
  22. 'info'=>$result['userinfo'],
  23. 'level'=>$result['level'],
  24. 'sessionid'=>$sessionid
  25. );
  26. echo json_encode($arr);
  27. }
  28. } else {
  29. $arr = array(
  30. 'flag'=>'empty',
  31. 'name'=>'',
  32. 'userid'=>'',
  33. 'sessionid'=>$sessionid
  34. );
  35. echo json_encode($arr);
  36. }
  37. ?>
复制代码
3.)数据库端(mysql)

采用mysql建立数据库,建立两个简单的数据表:user和info。

  1. /*
  2. MySQL Data Transfer
  3. Source Host: localhost
  4. Source Database: login
  5. Target Host: localhost
  6. Target Database: login
  7. Date: 2011-6-14 11:10:46
  8. */

  9. SET FOREIGN_KEY_CHECKS=0;
  10. -- ----------------------------
  11. -- Table structure for info
  12. -- ----------------------------
  13. CREATE TABLE `info` (
  14. `id` int(12) NOT NULL AUTO_INCREMENT,
  15. `userid` int(12) DEFAULT NULL,
  16. `userinfo` varchar(100) DEFAULT NULL,
  17. `level` int(2) DEFAULT NULL,
  18. PRIMARY KEY (`id`),
  19. KEY `useid` (`userid`),
  20. CONSTRAINT `useid` FOREIGN KEY (`userid`) REFERENCES `user` (`id`)
  21. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

  22. -- ----------------------------
  23. -- Table structure for user
  24. -- ----------------------------
  25. CREATE TABLE `user` (
  26. `id` int(12) NOT NULL AUTO_INCREMENT,
  27. `name` varchar(20) DEFAULT NULL,
  28. `password` varchar(20) DEFAULT NULL,
  29. `status` int(2) DEFAULT NULL,
  30. PRIMARY KEY (`id`)
  31. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

  32. -- ----------------------------
  33. -- Records
  34. -- ----------------------------
  35. INSERT INTO `info` VALUES ('1', '1', 'charlie  is a developer.', '1');
  36. INSERT INTO `info` VALUES ('2', '2', 'william is a boss.', '20');
  37. INSERT INTO `user` VALUES ('1', 'charlie', 'password', '1');
  38. INSERT INTO `user` VALUES ('2', 'william', 'mypassword', '2');

复制代码
运行效果如图:

图 -1 GetWebSession.java类的布局

图 -2 LoginSuccessActivity.java类获取的session id以及用户基本信息

图 -3 GetWebSession.java获取用户详细信息及本次session的一致性
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 多个dts文件音量不同怎么办 武装突袭3锁帧怎么办? 武装突袭3没子弹了怎么办 电脑系统注册表文件损坏怎么办 武装突袭3受伤了怎么办 模板跟旺铺版本不符合怎么办 cpu散热硅胶没了怎么办 武装突袭3掉帧怎么办 苹果系统占用内存太大怎么办 电脑上的文件打不开怎么办 word文件打开是乱码怎么办 wps文件打开是乱码怎么办 电脑读tst文件乱码怎么办 智能手机中了勒索病毒怎么办 部队训练脚崴了怎么办 滴滴车型信息识别失败怎么办 先打人被打了怎么办 我被警察打了怎么办 2k13选秀卡住了怎么办 正当防卫二进去之后没有字怎么办 国土防线2子弹打完了怎么办 诛仙宠物满了怎么办 试用联盟认证手机号是空号怎么办 系统出新版本不显示更新怎么办 炉石账号封停怎么办 电脑用户名和密码忘了怎么办 黑猴子棒球1闪退怎么办 斗米兼职报名后怎么办 婴儿足跟血检查有异常怎么办 打印机显示脱机使用打印机怎么办 手机刷机后内存变小了怎么办 吃了解毒丹喝酒怎么办 6s更新ios11失败怎么办 苹果6s手机发热怎么办 苹果手机没有描述文件怎么办 电脑玩穿越火线卡怎么办 老电脑又卡又慢怎么办 梦幻之星4花屏怎么办 失业连续好多天睡不着觉怎么办 工业废气排放总量超标怎么办 lol美服更新不了怎么办