gpt4 book ai didi

android - Android中webview与java http客户端的http session同步

转载 作者:可可西里 更新时间:2023-11-01 16:57:10 26 4
gpt4 key购买 nike

我正在使用 webview 和 native 开发混合应用程序。我在我的 webview 中使用 ajax post 方法,也在我的默认 android 代码上通过 HttpClient 使用 post 方法。但即使我去同一台服务器,我的 session ID 也不匹配。

有没有办法在我的应用程序的同一个 session 中发出 http 请求?感谢您的任何建议。

最佳答案

我已经解决了这个问题:

public void syncSession(final Context ctx){

new Thread(new Runnable(){
public void run(){

//Products will be stated in memory
ProductManager pm = ProductManager.getInstance();

// HttpClient httpclient = new DefaultHttpClient();
HttpPost httpget = new HttpPost(UrlConstants.SERVICE_URL_SYNC);
HttpResponse response;
String result = null;
try {
response = httpclient.execute(httpget);
//write db to

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



List<Cookie> cookies = httpclient.getCookieStore().getCookies();

if (! cookies.isEmpty()){

CookieSyncManager.createInstance(ctx);
CookieManager cookieManager = CookieManager.getInstance();

//sync all the cookies in the httpclient with the webview by generating cookie string
for (Cookie cookie : cookies){

Cookie sessionInfo = cookie;

String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
cookieManager.setCookie(UrlConstants.SERVICE_PRE_URL, cookieString);
CookieSyncManager.getInstance().sync();
}
}

}
}).start();
}

关于android - Android中webview与java http客户端的http session同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9795934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com