更新时间:2022-01-10 10:51:08浏览次数:923+次
以下带来HarmonyOS鸿蒙JAVA UI开发环境Webview怎么设置cookie和读取cookie的文章,将附上可用的代码。
前言
在鸿蒙开发中,可能会使用Webview去加载网页,需要将应用开发中使用到必要的cookie信息同步到HarmonyOS的webview,也有可能从HarmonyOS的webview中获取cookie信息,如下写一个demo作为参考,基础的webview学习。
1、设置cookie
我们需要重写webAgent的接口,实现isNeedLoadUrl的方法中设置如下代码:
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
mCookieStore.setCookieEnable(true);
mCookieStore.setCookie(url, "Domain="+"1111");
mCookieStore.setCookie(url, "Path=/");
mCookieStore.setCookie(url, "Value=00000");
2、读取cookie
我们需要重写webAgent的接口,实现onPageLoaded的方法中设置如下代码:
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
String cookiestr=mCookieStore.getCookie(url);
HiLogUtils.PrintLog(cookiestr);
3、整体代码,经测试完全可用
XML代码如下:
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<ohos.agp.components.webengine.WebView
ohos:id="$+id:webview"
ohos:height="0fp"
ohos:weight="1"
ohos:width="match_parent">
</ohos.agp.components.webengine.WebView>
</DirectionalLayout>
Java代码如下:
package com.harmony.alliance.mydemo.slice;
import com.harmony.alliance.mydemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.webengine.ResourceRequest;
import ohos.agp.components.webengine.WebAgent;
import ohos.agp.components.webengine.WebView;
public class WebviewSlice extends AbilitySlice {
private static final String EXAMPLE_URL = "https://www.jianshu.com/p/b6957bdbb21e/";
protected void onStart(Intent intent) {
super.onStart(intent);
setUIContent(ResourceTable.Layout_webview_slice);
WebView webView = (WebView) findComponentById(ResourceTable.Id_webview);
webView.getWebConfig().setJavaScriptPermit(true); // 如果网页需要使用JavaScript,增加此行;如何使用JavaScript请看相关的详细介绍
webView.setWebAgent(new WebAgent() {
@Override
public boolean isNeedLoadUrl(WebView webView, ResourceRequest request) {
if (request == null || request.getRequestUrl() == null) {
// LogUtil.info(TAG,"WebAgent isNeedLoadUrl:request is null.");
return false;
}
String url = request.getRequestUrl().toString();
//设置cookie
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
mCookieStore.setCookieEnable(true);
mCookieStore.setCookie(url, "Domain="+"WebviewSliceDomain");
mCookieStore.setCookie(url, "Path=/");
mCookieStore.setCookie(url, "Value=WebviewSliceValue");
if (url.startsWith("http:") || url.startsWith("https:")) {
webView.load(url);
return false;
} else {
return super.isNeedLoadUrl(webView, request);
}
}
@Override
public void onPageLoaded(WebView webView, String url) {
super.onPageLoaded(webView, url);
//获取cookie
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
String cookiestr=mCookieStore.getCookie(url);
HiLogUtils.PrintLog("cookiestr=====>>"+cookiestr);
}
});
webView.load(EXAMPLE_URL);
}
}
效果如下截图:
相关资讯