Android Couchbase SyncGateway Cookie

Android Couchbase SyncGateway Cookie,android,couchbase-lite,couchbase-sync-gateway,Android,Couchbase Lite,Couchbase Sync Gateway,萨拉姆 我这样做是为了在android项目中启用cookie,但在同步网关日志返回中: 401 login required 会话id(“6e2b5106712c0aa3e0048c5b724b302df63d5fbf”)的有效期为20年 couchbase服务器位于另一台本地pc(192.168.137.137) 解决的问题: try { syncUrl = new URL("http://192.168.137.137:4984/test"); } catch (Malformed

萨拉姆
我这样做是为了在android项目中启用cookie,但在同步网关日志返回中:

401 login required
会话id(“6e2b5106712c0aa3e0048c5b724b302df63d5fbf”)的有效期为20年
couchbase服务器位于另一台本地pc(192.168.137.137)

解决的问题:

try {
    syncUrl = new URL("http://192.168.137.137:4984/test");
} catch (MalformedURLException e) {
    throw new RuntimeException(e);
}

Replication pullReplication = database.createPullReplication(syncUrl);
pullReplication.setContinuous(true);

Replication pushReplication = database.createPushReplication(syncUrl);
pushReplication.setContinuous(true);

Authenticator auth = new BasicAuthenticator(username, password);
pushReplication.setAuthenticator(auth);
pullReplication.setAuthenticator(auth);

pullReplication.start();
pushReplication.start();

pullReplication.addChangeListener(this);
pushReplication.addChangeListener(this);

如果要使用cookie中提供的会话id,可以如下操作:

Map session=newhashmap();
会话put(“会话id”,“904AC010862F37C8DD9901A33AB5A3565FD8447”);
期次卖出(“到期”,“2015-09-23T17:27:17.555065803+01:00”);
会话put(“cookie_名称”、“SyncGatewaySession”);
DateFormat格式化程序=新的SimpleDataFormat(“yyyy-MM-dd'HH:MM:ss.SSSZ”);
日期=空;
试一试{
date=formatter.parse(session.get(“expires”);
}捕获(解析异常){
e、 printStackTrace();
}
复制pull=database.createPullReplication(syncGatewayURL);
pull.setCookie(session.get(“cookie\u名称”)、session.get(“session\u id”)、“/”、date、false、false);
pull.start();

验证不需要cookie吗?我们可以不使用cookie进行同步吗?
try {
    syncUrl = new URL("http://192.168.137.137:4984/test");
} catch (MalformedURLException e) {
    throw new RuntimeException(e);
}

Replication pullReplication = database.createPullReplication(syncUrl);
pullReplication.setContinuous(true);

Replication pushReplication = database.createPushReplication(syncUrl);
pushReplication.setContinuous(true);

Authenticator auth = new BasicAuthenticator(username, password);
pushReplication.setAuthenticator(auth);
pullReplication.setAuthenticator(auth);

pullReplication.start();
pushReplication.start();

pullReplication.addChangeListener(this);
pushReplication.addChangeListener(this);
Map<String, String> session = new HashMap<String, String>();
session.put("session_id", "904ac010862f37c8dd99015a33ab5a3565fd8447");
session.put("expires", "2015-09-23T17:27:17.555065803+01:00");
session.put("cookie_name", "SyncGatewaySession");

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = null;
try {
    date = formatter.parse(session.get("expires"));
} catch (ParseException e) {
    e.printStackTrace();
}

Replication pull = database.createPullReplication(syncGatewayURL);
pull.setCookie(session.get("cookie_name"), session.get("session_id"), "/", date, false, false);
pull.start();