BrowserField Cookie Deletion
I am making a Blackberry application using the RIM JRE 6. The first time a BrowserField is created, a cookie is created. At a certain moment (dependent on user input) the cookie must be deleted. To do this, I am opening a BrowserField and I am trying to delete cookies stored in the BrowserField. Currently, there are no methods I am aware of supplied in the RIM api's to explicitly delete cookies for BrowserField so I am using the executeScript method given in the BrowserField class to execute some javascript to remove the cookies. The javascript is executed in the onUiEngineAttached method before it runs the requestContent method. But this results in the application crashing with an IllegalStateException and the message:
Unable to execute script - script engine does not exist
I do not believe this is t开发者_如何学JAVAhe result of javascript not being initialized as on as it is supposed to be on by default from the BrowserFieldConfig class. I am unsure if I am formatting my javascript correctly though. This is the code I am using:
protected void onUiEngineAttached(boolean attached)
{
if(attached)
{
// ... Removed unnecessary code
// Retrieve the cookie manager for the embedded browser
BrowserFieldCookieManager cookieManager = _browserField.getCookieManager ();
if (eraseCookie == true)
{
String temp = cookieManager.getCookie ("someSite");
while (temp.indexOf ("testCookie") != -1)
{
_browserField.executeScript("javascript:document.cookie = \"testCookie=; expires=Thu, 01-Jan-70 00:00:01 GMT\"");
}
}
// Requests browser to go to URL with body and header information
_browserField.requestContent(_url, _body.getBytes(), _header);
}
};
It could possibly be a threading issue but I am not sure how to fix that. So any help is appreciated.
Thank you.
I have not used the BrowserFieldCookieManager interface as of yet, but have you tried calling setCookie( url, null ) or setCookie( url, "" ) on the cookie manager to clear out the cookie? I realize the API docs are sparse in this area. If neither of those work, try replacing the cookie with one whose time has expired already.
The other alternative is to substitute your own cookie manager when you create the BrowserField instance. See the docs for BrowserFieldConfig:
http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/browser/field2/BrowserFieldConfig.html
Well I cannot help you with BrowserFieldCookieManager but here is a very helpful website that got me started and supplied the scripts I have used for cookie management (if nothing else it should give you an idea what BrowserFieldCookieManager is doing behind the scenes and how to get it to do what you want).
http://www.quirksmode.org/js/cookies.html
Particularly look at how it deletes cookies, by replacing them with empty cookies that expire instantly.
Another way to not use cookies at all, if that is your intention, is by using this property inside BrowserFieldConfig
.
BrowserFieldConfig config = new BrowserFieldConfig();
config.setProperty(BrowserFieldConfig.ENABLE_COOKIES, Boolean.FALSE);
精彩评论