【问题标题】:Get Json response when Webview redirect to another page in Flutter当 Webview 重定向到 Flutter 中的另一个页面时获取 Json 响应
【发布时间】:2021-07-24 01:29:47
【问题描述】:

flutter_webview_plugin^0.3.11 我正在构建支付网关,在 3d 安全模式下我需要使用 Webview。使用 html 字符串类型的响应启动 Webview。

   return Scaffold(
  body: WebviewScaffold(
    withJavascript: true,
    key: _scaffoldKey,
    url: new Uri.dataFromString(widget.d3sc, mimeType: 'text/html').toString(),
  ),
);

它在屏幕上打印安全代码阶段。当我提交代码时,它会自动重定向到回调 url,我无法连接或从该 url 获得响应。我需要在提交代码并重定向页面以获取用于关闭 3d 安全支付的令牌时得到响应。有什么想法或解决方案吗?

试过下面的代码。虽然它打印了重定向的 url,但响应元素中没有 json 响应。

 webView.launch(new Uri.dataFromString(widget.d3sc, mimeType: 'text/html').toString() ,withJavascript: true, withLocalStorage: true);
  webView.show();

  webView.onUrlChanged.listen((String event) async {
    print("${event} even eevent");
    setState(() {
      webView
          .evalJavascript("document.documentElement.innerText")
          .then((response) => print("$response response response "));

    });
  });

【问题讨论】:

    标签: html flutter webview flutterwebviewplugin flutter-http


    【解决方案1】:

    我不知道我找到了解决方案,因为 html 内容非常复杂。通过使用webview_flutter插件问题进行排序。按原样构建您的小部件;

    WebView(
        initialUrl: new Uri.dataFromString(widget.d3sc, mimeType: 'text/html').toString(),
    
        onWebViewCreated: (controller) {
          _controller = controller;
        },
        javascriptMode: JavascriptMode.unrestricted,
        gestureNavigationEnabled: true,
        onPageFinished: (_) {
          readResponse();
        },
      )
    

    每当导航发生和页面打开完成时调用readResponse函数。我们可以使用以下函数将页面的 html 内容作为字符串获取;

        void readResponse() async
      {
      
    
        setState(() {
          _controller.evaluateJavascript("document.documentElement.innerHTML").then((value) async {
    
            if(value.contains("name=\"paymentId\"")){
    
    // value contains the html data of page as string
     ...
    

    我无法将 html 数据转换为 json 映射,但我们可以通过简单地使用 substring 方法在此字符串中达到我们想要的任何内容。

    注意:本例使用webview_flutter版本^1.0.7解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 2010-12-29
      • 2016-01-15
      • 2013-07-20
      相关资源
      最近更新 更多