我正在尝试维护我的Android应用程序和Drupal网站之间的登录用户会话。在我的研究中,它归结为将cookie发送回Drupal,但我很难找到一个资源来帮助指导我。
有人可以推荐一个很好的教程,我可以使用它来完成Android和Drupal之间的cookie驱动的持久连接吗?
答案 0 :(得分:1)
为了防止其他人遇到同样的问题,我遇到了类似的问题,我可以通过以下代码解决它:
1-在您的班级中定义CookieManager和CookieStore
CookieManager cookieManager;
CookieStore cookieStore;
2-添加默认的cookie处理程序,例如在类构造函数或OnCreate方法
中cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
3-执行HTTP请求时使用cookie存储
public byte[] openURI(String uri) {
try {
URI uriObj = new URI(uri);
DefaultHttpClient client = new DefaultHttpClient();
// Use the cookieStor with the request
if (cookieStore == null) {
cookieStore = client.getCookieStore();
} else {
client.setCookieStore(cookieStore);
}
HttpGet getRequest = new HttpGet(uriObj);
HttpResponse response = client.execute(getRequest);
// Read the response data
InputStream instream = response.getEntity().getContent();
int contentLength = (int) response.getEntity().getContentLength();
byte[] data = new byte[contentLength];
instream.read(data);
response.getEntity().consumeContent();
return data ;
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
答案 1 :(得分:0)
我很确定如果您使用随Android API提供的HttpClient,它应该为您进行会话管理,直到您手动关闭连接。
如果我错了,那么你可以通过使用CookieStore接口或BasicCookieStore类实现自己的cookie存储来轻松解决这个问题。如果所有其他方法都失败了,您可以手动存储cookie,并在每次发出HTTP请求时在标头中设置cookie。
我不确定这可能会因你的特定问题而改变,但考虑到你所提出问题的描述,这很可能会有所帮助。