我有一个Java / Jsp Web应用程序,在search_list.jsp页面上,我将结果设置在会话对象中,如下所示:
<%
Object resultdata = req.getSession().getAttribute("search_result");
if(resultdata == null)
{
resultdata = (SearchResult)getSearchResult();
request.getSession().setAttribute("search_result", resultdata);
}
%>
当我点击另一个页面并返回到search_list.jsp时,从Web浏览器从会话中检索结果对象工作正常,但相同的情况总是在android native webview中返回> resultdata null。以下是来自android native app的webview的代码。
public class HomeActivity implements OnClickListener {
public WebView mWebView;
public String sessionCookie = null;
protected void onCreate(Bundle savedInstanceState) {
mWebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webSettings.setLoadsImagesAutomatically(true);
mWebView.loadUrl(appPrefer.getAppURL());
mWebView.setWebViewClient(new WebViewClient());
}
public void saveSession()
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
if(mWebView!=null)
{
sessionCookie = cookieManager.getCookie(getCurrentURLLoad());
}
}
private void addSession(String url)
{
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
if (sessionCookie != null) {
cookieManager.setCookie(url, sessionCookie);
CookieSyncManager.getInstance().sync();
}
}
这是本机的按钮单击,其中本机(返回结果的快捷方式)是我再次加载search_list.jsp以显示结果。但是在网络级别,我们已经搜索并显示结果并查看profile.jsp页面。因此,在单击本地listview(URL为search_list.jsp)时,我添加了会话,如下所示:
public void onClick(View v) {
if(v.getId().== R.id.listview)
{
saveSession();
mWebView.loadUrl(HOST_URL + SEARCH_LIST );
addSession();
}
}
我的问题是它保存/传递了所有其他会话cookie值,但是设置为request.session.attribute的值不出现,它们都是> null,在从jsp级别单击native的url更改后,结果数据始终为null。
我已经尝试了答案中提到的方法,也尝试了-https://gist.github.com/arpit/610754方式,并且我总是看到我所有的cookie正确值(已经在我发布的que ..上工作了)..但是HTTPRequest会话中设置的属性不存在。我不认为cookie的一部分是httpRequets属性(HttpServletRequest->httpSession->setAttribute()),我的结论是它不在Android webview或httpclient端。请有人让我知道是正确的还是有一种方法可以读取http请求会话属性???
最佳答案
==============================这里的网页内容=====
您可能会发现自己在Android中所做的是网络身份验证。大多数网站都会发布cookie来跟踪会话ID并保持用户登录。为了维持此身份验证,您将需要在活动之间以及http客户端和webview之间保持cookie同步。
我最终做的方法似乎很有效,它是创建一个扩展Application的类,然后定义一个HttpClient以便在Application的其余部分中使用。该代码如下所示:
public class AppSettings extends Application {
private static final DefaultHttpClient client = createClient();
@Override
public void onCreate(){
}
static DefaultHttpClient getClient(){
return client;
}
private static DefaultHttpClient createClient(){
BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
httpclient.getCookieStore().getCookies();
return httpclient;
}
此类还创建一个适用于AsyncTasks的HttpClient,并同时发出多个http请求。通过将ThreadSafeClientConnManager用于ClientConnectionManager,我们可以在AsyncTasks中运行http请求,而无需在按下后退按钮并启动第二个或第三个按钮时强制关闭。
它还会创建一个默认的Cookie存储,可以在您的所有活动中对其进行访问。您可以通过调用getClient()方法在其他活动中使用它。
例如
public class SomeActivity extends Activity {
static DefaultHttpClient mClient = AppSettings.getClient();
...
try {
HttpGet httpget = new HttpGet('URL');
HttpResponse response;
response = mClient.execute(httpget);
...
}
...
}
如果您发现有必要使用Webview,并且需要它来访问和使用与您的客户端相同的cookie。您可以使用CookieManager来同步客户端的Cookie存储:(在onPageStarted方法中)
DefaultHttpClient mClient = AppSettings.getClient();
Cookie sessionInfo;
List<Cookie> cookies = mClient.getCookieStore().getCookies();
if (! cookies.isEmpty()){
CookieSyncManager.createInstance(getApplicationContext());
CookieManager cookieManager = CookieManager.getInstance();
for(Cookie cookie : cookies){
sessionInfo = cookie;
String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
cookieManager.setCookie("example.com", cookieString);
CookieSyncManager.getInstance().sync();
}
}
您将需要使用正确的域切换example.com。