Wednesday, December 13, 2017

Fill in web form from an app and submit

Leave a Comment

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.

4 Answers

Answers 1

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()"); 

Answers 2

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")); } 

Answers 3

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

Answers 4

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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment