Android WebView - loadUrl 方法的长度限制

loadUrl 方法的长度限制

  1. 在 Android WebView 开发中,使用 loadUrl 方法执行 Iavascript 代码并传递数据时,数据长度是受到限制的

  2. 数据长度通常被限制为不能超过 2MB


loadUrl 方法的长度限制演示

1、准备阶段
(1)Web
  • loadUrl.html
html 复制代码
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>loadUrl</title>
	</head>

	<body>
        <script>
			function printContentLength(content) {
				console.log("----- ", content.length);
			}
		</script>
    </body>
</html>
(2)Activity Layout
  • activity_load_url.xml
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoadUrlActivity"
    tools:ignore="MissingConstraints">

    <WebView
        android:id="@+id/wv_container"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
(3)Activity Code
  • LoadUrlActivity.java
java 复制代码
public class LoadUrlActivity extends AppCompatActivity {

    public static final String TAG = LoadUrlActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_url);

        WebView wvContainer = findViewById(R.id.wv_container);

        wvContainer.loadUrl("file:///android_asset/webview/loadUrl.html");

        WebSettings webSettings = wvContainer.getSettings();
        webSettings.setJavaScriptEnabled(true);

        wvContainer.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                wvContainer.loadUrl("javascript:printContentLength('hello world')");
            }
        });
    }
}
2、测试记录
  1. 传递长度为 1MB 的数据
java 复制代码
String str = new String(new char[1024 * 1024]).replace("\0", "a");

wvContainer.loadUrl("javascript:printContentLength('" + str + "')");
复制代码
# 输出结果

-----  1048576
  1. 传递长度为 2MB 的数据
java 复制代码
String str = new String(new char[2 * 1024 * 1024]).replace("\0", "a");

wvContainer.loadUrl("javascript:printContentLength('" + str + "')");
复制代码
# 输出结果

(无)

优化方案

1、基本介绍
  1. 优先使用 evaluateJavascript 方法来执行 JavaScript 代码,它没有字符数限制,执行效率更高,并能获取 JavaScript 的返回值

  2. 如果仍然使用 loadUrl 方法来执行较长的 JavaScript 代码,可以分片传输数据

2、演示
(1)使用 evaluateJavascript 方法
  1. 传递长度为 1MB 的数据
java 复制代码
String str = new String(new char[1024 * 1024]).replace("\0", "a");

wvContainer.evaluateJavascript("printContentLength('" + str + "')", null);
复制代码
# 输出结果

-----  1048576
  1. 传递长度为 2MB 的数据
java 复制代码
String str = new String(new char[2 * 1024 * 1024]).replace("\0", "a");

wvContainer.evaluateJavascript("printContentLength('" + str + "')", null);
复制代码
# 输出结果

-----  2097152
(2)分片传输数据
  • 传递长度为 2MB 的数据
js 复制代码
let contents = null;

function printContentLength(content, chunkNum, chunkTotal) {
    if (contents == null) contents = [];
    contents.push(content);
    if (chunkNum != chunkTotal) return;
    console.log("----- ", contents.join("").length);
}
java 复制代码
String str = new String(new char[2 * 1024 * 1024]).replace("\0", "a");

// 分片传输字符串
int chunkSize = 1024 * 1024; // 1MB 分片
int chunkTotal = (int) Math.ceil((double) str.length() / chunkSize);
for (int i = 0; i < chunkTotal; i++) {
    int start = i * chunkSize;
    int end = Math.min(start + chunkSize, str.length());
    String chunk = str.substring(start, end);
    wvContainer.loadUrl(String.format("javascript:printContentLength('%s', %d, %d)",
            chunk, i + 1, chunkTotal));
}
复制代码
# 输出结果

-----  2097152
相关推荐
Resean02233 小时前
架构设计系列(十六):DDD 与代码架构
java·开发语言·架构
学长毕设3 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
Flynt5 小时前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
三少爷的鞋6 小时前
为什么成熟 Android 项目几乎看不到 IdleHandler?
android
kururunga7 小时前
Android知识点回顾之蓝牙协议
android·蓝牙·车载开发
StarsGreen8 小时前
aosp中的SELinux 策略配置和编译流程
android
她的男孩9 小时前
Spring Boot 接 Flowable:请假单要同时走财务和行政,包容分支和默认分支怎么配
java·spring boot·工作流引擎
咖啡八杯9 小时前
LoginUser 适配器模式:适配 SysUser 到 UserDetails
java·开发语言·后端·框架·适配器模式·若依
北冥有鱼10 小时前
从 bean::method 里抠出注解:Java 方法引用元数据反查全记录
java·后端
CoderYanger11 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis