简体   繁体   English

如何将数据从在 kotlin 上运行的 android 应用程序发送到在 webview 上打开的 react js 应用程序?

[英]how to send data from android application running on kotlin to a react js application which is opened on webview?

Suppose you are running an android application (kotlin) and u have logged in and have some data.假设您正在运行一个 android 应用程序 (kotlin) 并且您已登录并拥有一些数据。 Then in webview you open a react js application.然后在 webview 中打开一个 react js 应用程序。 How can u pass that login data from android to react js application.你如何从 android 传递登录数据来响应 js 应用程序。

I haven't done this personally, but I believe you would want to package any data you have inside of a script tag and inject it into the web view.我没有亲自做过这件事,但我相信您会想要将您拥有的任何数据打包到脚本标记中并将其注入到 Web 视图中。 For example, if the React application needs the value of user_id you would pass in the user id to a script like:例如,如果 React 应用程序需要user_id的值,您可以将用户 id 传递给如下脚本:

val script = """
    <script> var user_id = ${user.id} </script>
"""

Because I'm not a React guy, that may not work right off the bat.因为我不是 React 人,所以这可能无法立即生效。 You might need to add the value to the document object and read it off that way.您可能需要将该值添加到document对象并以这种方式读取它。

Once the script is created then you can include it into your webview using the loadDataWithBaseURL function .创建脚本后,您可以使用loadDataWithBaseURL函数将其包含到您的loadDataWithBaseURL It would look something like this:它看起来像这样:

webView.loadDataWithBaseURL(reactAppUrl, script, "text/html", "utf-8", null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 mySQL 数据库(在不支持 Node.js 的主机上)连接到 React 应用程序(JS)? - How to connect mySQL data base (on hosting which does not support Node.js) to React application (JS)? 从 React 应用程序发送电子邮件 - Send email from React application 如何从android应用程序的webview访问设备摄像头 - how to access the device camera from webview of android application 如何从反应应用程序停止在 node.js 上运行异步 function? - How to stop running async function on node.js from react application? 如何将一个 React.js 页面的值发送到另一个页面,两者都是独立打开的? - How need to send a value from one React.js page to another page both are opened independently? 如何解决Webview Browser Android应用程序中的错误? - How Solve error in Webview Browser Android Application? 如何从Sencha Touch应用程序打开Android应用程序并发送一些数据? - How to open Android app from Sencha Touch Application and send some data? 如何在响应原生android应用程序中实现Headless JS? - How to implement Headless JS in react native android application? 如何将数据从节点 js 发送到 axios 以响应 js - How to send data from node js to axios to react js 在托管应用程序中打开时,React js应用程序的CSS样式将被覆盖 - React js application's css styling be overriden when opened in hosted application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM