1

アプリケーションを閉じた後もセッションを維持できるようにするには、Cookie を保存する必要があります。そのためには、Cookie を保存してから再作成できるようにする必要があります。私の現在の問題は、 BasicClientCookie を正しく作成できないことです。ドメインが null であるか、他のデータが null です。

テストコード:

List<Cookie> cookies = this.httpClient.getCookieStore().getCookies();
if (cookies != null) {
    Log.print("SAVING TO COOKIE CACHE");
    for (Cookie cookie : cookies) {
        String cookieString = cookie.getName() + "=" + cookie.getValue()
                + "; domain=" + cookie.getDomain();
        Log.d("Saving cookie: %s", cookieString);
        String[] keyValueSets = cookieString.split(";");
        if (keyValueSets.length == 0 || keyValueSets[0] == null) {
            break;
        }
        String[] keyValue = keyValueSets[0].split("=");
        if (keyValue.length == 0 || keyValue[0] == null) {
            break;
        }
        String key = keyValue[0];
        Log.d("Adding cookie with key %s and value %s", key, cookieString);
            Cookie co = new BasicClientCookie(key, cookieString);
        Log.print("Cookie domain: " + co.getDomain());
        Log.print("Cookie name: " + co.getName());
        Log.print("Cookie val: " + co.getValue());
        Log.print("Cookie comment: " + co.getComment());
        Log.print("Cookie commentUrl: " + co.getCommentURL());
        Log.print("Cookie path: " + co.getPath());
        Log.print("Cookie version: " + co.getVersion());
        Log.print("Cookie date: " + co.getExpiryDate());
        CookieManager.getInstance().setCookie(cookie.getDomain(), cookieString);
        this.cacheManager.setString(TAG, cookie.getDomain());
    }
}
CookieSyncManager.getInstance().sync();

LogCat:

03-28 13:56:35.405: E/PRINTER(9714): SAVING TO COOKIE CACHE
03-28 13:56:35.405: D/Debug(9714): Saving cookie: ci_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22123e722edd173a295c1e8f3efae7c0ba%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%2287.32.150.212%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1364471789%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D784e1b95d3e5b11fb969d137a67676bb; domain=test.address.lt
03-28 13:56:35.405: D/Debug(9714): Adding cookie with key ci_session and value ci_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22123e722edd173a295c1e8f3efae7c0ba%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%2287.32.150.212%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1364471789%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D784e1b95d3e5b11fb969d137a67676bb; domain=test.address.lt
03-28 13:56:35.405: E/PRINTER(9714): Cookie domain: null
03-28 13:56:35.405: E/PRINTER(9714): Cookie name: ci_session
03-28 13:56:35.405: E/PRINTER(9714): Cookie val: ci_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22123e722edd173a295c1e8f3efae7c0ba%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%2287.32.150.212%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1364471789%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D784e1b95d3e5b11fb969d137a67676bb; domain=test.address.lt
03-28 13:56:35.405: E/PRINTER(9714): Cookie comment: null
03-28 13:56:35.405: E/PRINTER(9714): Cookie commentUrl: null
03-28 13:56:35.405: E/PRINTER(9714): Cookie path: null
03-28 13:56:35.405: E/PRINTER(9714): Cookie version: 0
03-28 13:56:35.405: E/PRINTER(9714): Cookie date: null
03-28 13:56:35.890: D/Debug(9714): Saving cookie: PHPSESSID=97eotn8964tgvv9j80uk8dddh5; domain=test.address.lt
03-28 13:56:35.890: D/Debug(9714): Adding cookie with key PHPSESSID and value PHPSESSID=97eotn8964tgvv9j80uk8dddh5; domain=test.address.lt
03-28 13:56:35.890: E/PRINTER(9714): Cookie domain: null
03-28 13:56:35.890: E/PRINTER(9714): Cookie name: PHPSESSID
03-28 13:56:35.890: E/PRINTER(9714): Cookie val: PHPSESSID=97eotn8964tgvv9j80uk8dddh5; domain=test.address.lt
03-28 13:56:35.890: E/PRINTER(9714): Cookie comment: null
03-28 13:56:35.890: E/PRINTER(9714): Cookie commentUrl: null
03-28 13:56:35.890: E/PRINTER(9714): Cookie path: null
03-28 13:56:35.890: E/PRINTER(9714): Cookie version: 0
03-28 13:56:35.890: E/PRINTER(9714): Cookie date: null

私は何を間違っていますか?

4

2 に答える 2

2

BasicClientCookie に関する適切なドキュメントが見つからなかったため、getDomain()、getValue() などのすべてのコンポーネントを、プログラムを閉じるときに特別なシンボル シーケンスで区切られた文字列に格納し、後でプログラムを再開したときに、その文字列をメモリからロードし、個別の値を解析し、setDomain()、setValue() などのメソッドを使用して有効な Cookie を作成します。これは今のところ問題なく動作しているようです...

于 2013-03-29T12:50:56.770 に答える
0

セッションを維持して Cookie を保存するには、実際に Cookie インターフェイスを実装できます。例を次に示します。

package com.transit.http;

import java.util.Date;
import org.apache.http.cookie.Cookie;

public class TransitCookie implements Cookie {

private String name;
private String value;
private String comment;
private String commentURL;
private String domain;
private String path;
private Date expiryDate;
private boolean persistent;
private boolean secure;
private int[] ports;
private int version;

@Override
public String getName() {

    return name;
}

@Override
public String getValue() {
    return value;
}

@Override
public String getComment() {
    return comment;
}

@Override
public String getCommentURL() {
    return commentURL;
}

@Override
public Date getExpiryDate() {
    return expiryDate;
}

@Override
public boolean isPersistent() {
    return persistent;
}

@Override
public String getDomain() {
    return domain;
}

@Override
public String getPath() {
    return path;
}

@Override
public int[] getPorts() {
    return ports;
}

@Override
public boolean isSecure() {
    return secure;
}

@Override
public int getVersion() {
    return version;
}

@Override
public boolean isExpired(Date date) {
    if (expiryDate != null) {
        return date.after(expiryDate);
    }
    /**
     * In this case, we must return false otherwise this cookie will be
     * rejected
     */
    return false;
}

public void setName(String name) {
    this.name = name;
}

public void setValue(String value) {
    this.value = value;
}

public void setComment(String comment) {
    this.comment = comment;
}

public void setCommentURL(String commentURL) {
    this.commentURL = commentURL;
}

public void setDomain(String domain) {
    this.domain = domain;
}

public void setPath(String path) {
    this.path = path;
}

public void setExpiryDate(Date expiryDate) {
    this.expiryDate = expiryDate;
}

public void setPersistent(boolean persistent) {
    this.persistent = persistent;
}

public void setSecure(boolean secure) {
    this.secure = secure;
}

public void setPorts(int[] ports) {
    this.ports = ports;
}

public void setVersion(int version) {
    this.version = version;
}

}

シリアライズしたい場合は、java.io.Serializable. 幸運を。

于 2015-12-16T23:52:02.513 に答える