【问题标题】:Fill form programmatically in Android WebView in the background (Work manager)在后台以编程方式在 Android WebView 中填写表单(工作管理器)
【发布时间】:2022-11-13 00:55:17
【问题描述】:

我正在尝试以编程方式创建一个应用程序填写表格并在后台提交...

我已经用 web_view 实现了它,它工作得很好,但它在一个活动中,但我想从后台做同样的事情。

由于网络视图是一个 UI 元素,我不知道如何在工作人员中运行它!

我的代码(仅在应用程序打开时有效):

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // Calling the composable function
            // to display element and its contents
            MainContent()
        }
    }
}

// Creating a composable
// function to display Top Bar
@Composable
fun MainContent() {
    Scaffold(
        topBar = { TopAppBar(title = { Text("WebView", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
        content = { MyContent() }
    )
}


@SuppressLint("SetJavaScriptEnabled")
@Composable
fun MyContent(){
    val context = LocalContext.current
    // Declare a string that contains a url
//        val mUrl = "http:www.example.com"

    // Adding a WebView inside AndroidView
    // with layout as full screen
    AndroidView(factory = {
        WebView(it).apply {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )



            webViewClient = object: WebViewClient(){

                override fun onPageFinished(view: WebView?, url: String?) {
                    super.onPageFinished(view, url)


  evaluateJavascript("const inputList=document.querySelectorAll(\"input\");\n" +
            "inputList[0].value=\"00000\";\n" +
            "inputList[1].value=\"00000\";\n" +
            "inputList[2].click()", null)


                }

            }
            settings.javaScriptEnabled = true
            loadUrl(mUrl)
            //enable javascript



        }
    }, update = {
        it.loadUrl(mUrl)
    })
}

// For displaying preview in
// the Android Studio IDE emulator
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MainContent()
}

所以我的问题是:

1.我可以在工作人员(WorkManager)中运行网络视图吗?如何?

2. 有没有其他方法可以在没有网络视图的情况下做到这一点?

【问题讨论】:

  • 我想知道你为什么使用 webview。只需使用 http 组件即可发布内容。 OkHttp,改造,HttpUrlConnection..
  • @blackapps 感谢您的回复...非常感谢...在该站点上,页面上的 javascript 在提交之前将文本输入转换为 md5 哈希。我认为无头浏览器可以正常工作,但经过 2 天的搜索,我没有找到任何适用于 android 的无头浏览器。直接提交表格在我的情况下不起作用...... N.B.我是安卓开发新手
  • 您还可以在提交之前对所有输入文本进行 md5 编码。但是......接收者可以用 md5 哈希做什么?这是干什么用的?
  • @blackapps我正在使用一个开放的wifi,每次我将手机连接到网络时都需要一个网络基础登录(强制门户)。而且我必须使用 android 强制门户网站使用我的 ID 和密码登录...我正在尝试自动化此过程,以便每当我连接到 wifi 时,它都会自动使用我的 ID 和密码登录。
  • 我尝试使用邮递员将数据发布到服务器以测试它是否有效,但它没有......经过一些研究(使用 chrome dev 工具),我意识到该网站以某种方式将输入转换为 md5在发布之前哈希...这意味着我无法发布痛苦文本...这就是我首先使用网络视图的原因...您现在知道如何使用这些吗?

标签: android webview android-workmanager


【解决方案1】:

对于我的情况,Htmlunit-android 工作得很好......它可以无头地浏览网页并可以提交表单。

表单提交示例:

private fun submittingForm() {
       
        val webClient = WebClient()

            // Get the first page
            val page1 = webClient.getPage<HtmlPage>("http://some_url")

            // Get the form that we are dealing with and within that form,
            // find the submit button and the field that we want to change.
            val form = page1.getFormByName("myform")
            val button = form.getInputByName<HtmlSubmitInput>("submitbutton")
            val textField = form.getInputByName<HtmlTextInput>("userid")

            // Change the value of the text field
            textField.type("root")

            // Now submit the form by clicking the button and get back the second page.
            val page2 = button.click<HtmlPage>()
        }

【讨论】:

    猜你喜欢
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多