Aishvarya Jaiswal
Aishvarya Jaiswal

Reputation: 1811

Autofill webview form

I have a webview which opens this website. As we can see it has an input field. I want to autofill that input field with my data and auto click on submit button. Here is what I am trying for autofill but its not working.

public class WebViewActivity extends AppCompatActivity {
    private WebView webView;
    String url = "http://www.indianrail.gov.in/pnr_Enq.html", pnrNumber = "";
    ProgressBar pb;
    Toolbar toolbar;
    private Tracker mTracker;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_website);

        init();
    }

    private void init() {


        pnrNumber = getIntent().getStringExtra("pnrNumber");

        pb = (ProgressBar) findViewById(R.id.progressBar);
        pb.getProgressDrawable().setColorFilter(ContextCompat.getColor(this, R.color.White), PorterDuff.Mode.SRC_IN);

        webView = (WebView) findViewById(R.id.webView);

        this.webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);

        webView.getSettings().setSupportMultipleWindows(false);
        webView.getSettings().setDomStorageEnabled(true);

        webView.loadUrl(url);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                pb.setProgress(progress);
                if (progress == 100) {
                    pb.setProgress(0);
                }
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView v, String url) {
                v.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView v, String url) {
                v.loadUrl("javascript:document.forms[0].lccp_pnrno1.value = '"+pnrNumber+"';");
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mTracker.setScreenName("WebView Fragment with " + url);
        mTracker.send(new HitBuilders.ScreenViewBuilder().build());

    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        webView.stopLoading();
    }

    @Override
    public void onStop() {
        super.onStop();
        // The rest of your onStop() code.
        webView.stopLoading();
    }
}

Am I missing something.?

Upvotes: 1

Views: 374

Answers (1)

Janno
Janno

Reputation: 542

If you want to autofill and submit it straight away, why don't you just already send a POST request straight to that page to get the contents you desire?

All you gotta do, is do a POST request to this page:

http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi_8238.cgi

With POST data: lccp_cap_value, lccp_capinp_value, lccp_pnrno1

The pnrno1 is the value you input there. The cap value and capinp values for me are 24357. Recheck that are they the same for you as well. Not sure that are they dynamic or static.

Other than that, I think that it's not possible to achieve this without hooking some kind of an external JavaScript file to it. But the POST solution should work (if the cap and capinp values are not dynamic).

Upvotes: 1

Related Questions