从应用程序填写网络表格并提交

Fill in web form from an app and submit

我有一个带有简单表单的网页,其中包含 3 个输入字段、姓名、电子邮件和课程以及一个提交按钮。没什么好看的。

在开发 android 应用程序时,我想知道如何在不使用网络视图实际加载网页的情况下从我的 android 应用程序中填写该 Web 表单。

我以前用过类似的东西。但是,您将必须加载一个 webview(尽管您不需要显示它。)然后执行如下操作。请记住,如果网站更改了 id,此方法将不起作用。

webView.loadUrl("javascript:var 
    name=document.getElementById('nameInput').value='" yourNameValue "'");
webView.loadUrl("javascript:document.getElementById('submit').click()");

在您的 build.gradle(Module:app)

中添加此依赖项
dependencies {
implementation 'com.android.support:customtabs:27.0.2'
}

在Activity中添加此方法。

public boolean isPackageExisted(String targetPackage){
PackageManager pm=getPackageManager();
    try {
        PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
    return true;
}

添加这个 onCreate of Activity

if (isPackageExisted("com.android.chrome")){
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();                        
customTabsIntent.intent.setPackage("com.android.chrome");                             
customTabsIntent.intent.setAction(Intent.ACTION_VIEW);                             
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                          
customTabsIntent.launchUrl(getApplicationContext(), Uri.parse("Your URL"));
}

不使用网络视图就无法将页面加载到移动应用程序中。或者你可以:

  1. 在 android 中使用输入
  2. 创建一个表单
  3. 设置网络服务以将数据提交到

有关分步指南,请参阅 this android simple form submit tutorial

使用 Volley 做请求: https://developer.android.com/training/volley/index.html 然后你可以使用类型为 multipart-form-data 的 post ;),同时查看表单的 "action" 字段,这样你就可以知道 post 请求必须发送到哪里并发送正如我所说,通过 multipart-form-data 的数据。

如果没有 WebView,您将无法加载网页,或者您可以在 java 中创建 html 页面。 您可以通过隐藏 url 地址将网页表示为您的应用程序页面 Disable address bar in Android webview

或者你想从 url 中读取字符串意味着使用这个 Get text from web page to string

这个更好

    WebView web = (WebView)findViewById(R.id.web1);
    web.getSettings().setJavaScriptEnabled(true);
    web.getSettings().setDomStorageEnabled(true);
    web.loadUrl("url_address");
    web.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            web.loadUrl("javascript:document.getElementById('username').value = 'alireza'; null");
            web.loadUrl("javascript:document.getElementById('password').value = '12345678'; null");
            web.loadUrl("javascript:document.getElementById('login_button').click(); null");
        }
    });