8

I have a webpage with a simple form with 3 input fields, Name, Email and Course and a submit button. Nothing fancy.

Upon developing an android app, I would like to know how I could fill in that web form from within my android app without using a webview to actually load the webpage.

Alin
  • 1,218
  • 5
  • 21
  • 47
  • You will need to implement webservices on the server side and consume them on the client side. – Phantômaxx Nov 20 '17 at 15:26
  • I would use Jsoup to do it. The implentation depends on the actual page. – TDG Dec 08 '17 at 15:43
  • 1
    is what u mean with ur app using intent action view open browser and fill the form? or just to pass paramater like api does? using get is more reasonable if u will use apps default browser the u can redirecting to page base on get parameter u already pass – Iqbal Rizky Dec 11 '17 at 04:40
  • @IqbalRizky no, using am intent would be so much easier. Juat pass the parameter like an api. – Alin Dec 11 '17 at 09:10

6 Answers6

4

I have used something like this in the past. However you will have to load a webview (though you dont need to show it.) then do something like below. Keep in mind that if the site changes the id down the road this approach will not work.

webView.loadUrl("javascript:var 
    name=document.getElementById('nameInput').value='" yourNameValue "'");
webView.loadUrl("javascript:document.getElementById('submit').click()");
Edward DiGirolamo
  • 742
  • 1
  • 5
  • 18
1

Add this dependency in your build.gradle(Module:app)

dependencies {
implementation 'com.android.support:customtabs:27.0.2'
}

Add this method in 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;
}

Add this 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"));
}
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
1

There is no possible way of loading a page into a mobile app without using a webview. Alternatively you can:

  1. Create a form in android with the inputs
  2. Set up a webservice to submit data to

See this android simple form submit tutorial for a step-by-step guide

William
  • 740
  • 2
  • 11
  • 18
1

Use Volley to do the request: https://developer.android.com/training/volley/index.html Then you can use a post with type multipart-form-data ;), also look at the "action" field of the form so you can know where the post request must be sent and send the data through multipart-form-data as I said.

Josema
  • 445
  • 2
  • 6
  • 22
1

This is better

    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");
        }
    });
Alireza
  • 11
  • 2
0

Without WebView you can't load the webpage or you can create html page in java. you can represent webpage as your app page by hiding the url address Disable address bar in Android webview

or you want read the string from the url means use this Get text from web page to string

Vishnu
  • 1