Java Android会话管理
是否有特定的Android会话管理库?我需要在一个普通的Android应用程序中管理我的会话。不在Java Android会话管理,java,android,session,session-cookies,apache-commons-httpclient,Java,Android,Session,Session Cookies,Apache Commons Httpclient,是否有特定的Android会话管理库?我需要在一个普通的Android应用程序中管理我的会话。不在WebView中。我可以通过post方法设置会话。但当我发送另一个请求时,会话将丢失。有人能帮我处理这件事吗 DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("My url"); HttpResponse response = httpClient.execute(ht
WebView
中。我可以通过post方法设置会话。但当我发送另一个请求时,会话将丢失。有人能帮我处理这件事吗
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("My url");
HttpResponse response = httpClient.execute(httppost);
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
我得到了登录页面的响应正文。这与Android无关。它与ApacheHttpClient有关,ApacheHttpClient是用于HTTP访问的库 会话cookie存储在
DefaultHttpClient
对象中。不要为每个请求创建一个新的DefaultHttpClient
,而是保留它并重用它,您的会话cookie将得到维护
您可以阅读ApacheHttpClient和HttpClient中的cookie管理。这是我在帖子中使用的内容。我可以使用此方法使用新的
httpClients
,其中phpsessid
是使用上述代码从登录脚本中提取的PHP
会话id
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("PHPSESSID",phpsessid));
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“PHPSESSID”,PHPSESSID));
通常,在Java HttpURLConnection中,您可以通过这种方式设置/获取cookie(以下是整个连接过程)。下面的代码位于我的ConnectionRead的run()中,所有连接活动类都从中继承。所有共享与所有请求一起发送的公共静态sCookie字符串。因此,您可以保持登录/关闭等常见状态:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//set cookie. sCookie is my static cookie string
if(sCookie!=null && sCookie.length()>0){
conn.setRequestProperty("Cookie", sCookie);
}
// Send data
OutputStream os = conn.getOutputStream();
os.write(mData.getBytes());
os.flush();
os.close();
// Get the response!
int httpResponseCode = conn.getResponseCode();
if (httpResponseCode != HttpURLConnection.HTTP_OK){
throw new Exception("HTTP response code: "+httpResponseCode);
}
// Get the data and pass them to the XML parser
InputStream inputStream = conn.getInputStream();
Xml.parse(inputStream, Xml.Encoding.UTF_8, mSaxHandler);
inputStream.close();
//Get the cookie
String cookie = conn.getHeaderField("set-cookie");
if(cookie!=null && cookie.length()>0){
sCookie = cookie;
}
/* many cookies handling:
String responseHeaderName = null;
for (int i=1; (responseHeaderName = conn.getHeaderFieldKey(i))!=null; i++) {
if (responseHeaderName.equals("Set-Cookie")) {
String cookie = conn.getHeaderField(i);
}
}*/
conn.disconnect();
在Android应用程序中保持会话活动(用户登录或其他)的完全透明方式。 它在单例和HttpRequest/Response拦截器中使用apachedefaulthttpclient SessionKeeper类只检查其中一个头是否设置为Cookie,如果设置为Cookie,它只会记住它。 SessionAdder只是将会话id添加到请求中(如果不是null)。 这样,您就可以知道整个身份验证过程是完全透明的
public class HTTPClients {
private static DefaultHttpClient _defaultClient;
private static String session_id;
private static HTTPClients _me;
private HTTPClients() {
}
public static DefaultHttpClient getDefaultHttpClient(){
if ( _defaultClient == null ) {
_defaultClient = new DefaultHttpClient();
_me = new HTTPClients();
_defaultClient.addResponseInterceptor(_me.new SessionKeeper());
_defaultClient.addRequestInterceptor(_me.new SessionAdder());
}
return _defaultClient;
}
private class SessionAdder implements HttpRequestInterceptor {
@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
if ( session_id != null ) {
request.setHeader("Cookie", session_id);
}
}
}
private class SessionKeeper implements HttpResponseInterceptor {
@Override
public void process(HttpResponse response, HttpContext context)
throws HttpException, IOException {
Header[] headers = response.getHeaders("Set-Cookie");
if ( headers != null && headers.length == 1 ){
session_id = headers[0].getValue();
}
}
}
}
非常感谢你。。我会试试的谢谢你的软件,它真的解决了我的问题。非常感谢……:)此链接给出了如何实现此功能的想法
public class HTTPClients {
private static DefaultHttpClient _defaultClient;
private static String session_id;
private static HTTPClients _me;
private HTTPClients() {
}
public static DefaultHttpClient getDefaultHttpClient(){
if ( _defaultClient == null ) {
_defaultClient = new DefaultHttpClient();
_me = new HTTPClients();
_defaultClient.addResponseInterceptor(_me.new SessionKeeper());
_defaultClient.addRequestInterceptor(_me.new SessionAdder());
}
return _defaultClient;
}
private class SessionAdder implements HttpRequestInterceptor {
@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
if ( session_id != null ) {
request.setHeader("Cookie", session_id);
}
}
}
private class SessionKeeper implements HttpResponseInterceptor {
@Override
public void process(HttpResponse response, HttpContext context)
throws HttpException, IOException {
Header[] headers = response.getHeaders("Set-Cookie");
if ( headers != null && headers.length == 1 ){
session_id = headers[0].getValue();
}
}
}
}