Http请求保存用户的的登录状态

今天是关于怎么处理,用户登录之后,怎么保持登录状态。


HTTP 用户登录的过程

A 客户登录之,Web服务器会给该客服端产生一个SessionID

B SessionID 会随着服务器的响应结果一并返回来

C 客服端获取服务器返回来的SessionID

D 向服务器发送其他请求,要把SessionID传给服务器,服务器会去匹配该SessionID是否存在,如果存在就是登录,如果不存在,则是未登录




1 如何获取Web服务器返回来的SessionID

 

/**
	 * 获取WeB服务器Sessionid
	 */
	public static void getSessionID(HttpUtils http) {
		DefaultHttpClient defa = (DefaultHttpClient) http.getHttpClient();
		CookieStore cookie = (CookieStore) defa.getCookieStore();
		List list = cookie.getCookies();
		for (int i = 0; i < list.size(); i++) {
			
			String name = list.get(i).getName();//得到Sessionid  Key值

			if (name.equals("JESSIONID")) {

				SessionClass.SessionID = list.get(i).getValue();//得到Sessionid  value值
				Log.i("Sessionid", SessionClass.SessionID);
				
				break;
			}
		}
	}


2 把获取到的Sessionid 存起来

	/**
	 * SessionID存起来
	 * 
	 * @param context
	 * @param sessionid
	 * @param http
	 */
	private static void setCookie(Context context) {
		if (context != null) {
			if (!TextUtils.isEmpty(SessionClass.SessionID)) {
				BasicClientCookie cookie = new BasicClientCookie("JESSIONID",
						SessionClass.SessionID);
				cookie.setVersion(0);
				cookie.setDomain("tr.zzapi.gson.cn");
				cookie.setPath("/");
				BasicCookieStore cook = new BasicCookieStore();
				cook.addCookie(cookie);
				http.configCookieStore(cook);

			}
		}
	}


3 请求是带上Sessionid

				 HttpUtils http = SessionClass
						.getHttp(SessionID_main.this);




步骤   先获得SessionID      然后 存到 本地     最后 请求带上 本地的SessionID





完整的例子

package com.xcl.android;

import java.util.List;

import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.SetCookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.lidroid.xutils.HttpUtils;

public class SessionClass {
	private static HttpUtils http = new HttpUtils();
	private static String SessionID;
	/**
	 * 单利模式 只创立一次 这个必须要
	 * 
	 * @param context
	 * @return
	 */
	public static HttpUtils getHttp(Context context) {
		if (http == null) {
			http = new HttpUtils();
		}
		setCookie(context);
		return http;
	}

	/**
	 * 获取WeB服务器Sessionid
	 */
	public static void getSessionID(HttpUtils http) {
		DefaultHttpClient defa = (DefaultHttpClient) http.getHttpClient();
		CookieStore cookie = (CookieStore) defa.getCookieStore();
		List list = cookie.getCookies();
		for (int i = 0; i < list.size(); i++) {
			
			String name = list.get(i).getName();//得到Sessionid  Key值

			if (name.equals("JESSIONID")) {

				SessionClass.SessionID = list.get(i).getValue();//得到Sessionid  value值
				Log.i("Sessionid", SessionClass.SessionID);
				
				break;
			}
		}
	}

	/**
	 * SessionID存起来
	 * 
	 * @param context
	 * @param sessionid
	 * @param http
	 */
	private static void setCookie(Context context) {
		if (context != null) {
			if (!TextUtils.isEmpty(SessionClass.SessionID)) {
				BasicClientCookie cookie = new BasicClientCookie("JESSIONID",
						SessionClass.SessionID);
				cookie.setVersion(0);
				cookie.setDomain("tr.zzapi.gson.cn");
				cookie.setPath("/");
				BasicCookieStore cook = new BasicCookieStore();
				cook.addCookie(cookie);
				http.configCookieStore(cook);

			}
		}
	}
}




package com.xcl.android;

import java.util.List;

import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sessionid.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class SessionID_main extends Activity {
	private TextView sessionID;

	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sessionid);
		sessionID = (TextView) findViewById(R.id.text);
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				final HttpUtils http = SessionClass
						.getHttp(SessionID_main.this);
				RequestParams params = new RequestParams();
				params.addBodyParameter("userName", "李四");
				params.addBodyParameter("password", "q");
				http.send(HttpMethod.POST,
						"http://tr.zzapi.gson.cn/api/user/login", params,
						new RequestCallBack() {

							@Override
							public void onFailure(HttpException arg0,
									String arg1) {
								// TODO Auto-generated method stub

							}

							@Override
							public void onSuccess(ResponseInfo arg0) {
								// TODO Auto-generated method stub

								SessionClass.getSessionID(http);
							}
						});
			}
		});
	}

	
}








你可能感兴趣的:(保持登录状态,Xutlis)