Home » Android » Post data to webview in android

Post data to webview in android

Posted by: admin June 15, 2020 Leave a comment

Questions:

Does anyone know how to pass value in webview using post method?

engine = (WebView) findViewById(R.id.web_engine);

engine.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view, url);
    }
});

engine.getSettings().setJavaScriptEnabled(true);
engine.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
engine.loadUrl(url+"?ref=api&uname="+id+"&pass="+pass);

This is the way I am sending the data using get method, I want to use Post method.

How to&Answers:

Try this

String url = "http://www.example.com";
String postData = "username=my_username&password=my_password";
webview.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));

Answer:

Use postUrl to use POST in WebView.

Answer:

Use postUrl Method for post dat. More detail Follow This.

Answer:

Better with Base64.encode:

String url = "http://www.example.com";
String postData = "username=my_username&password=my_password";
webview.postUrl(url,Base64.encode(postData.getBytes(), Base64.DEFAULT));

In some cases works better:

String url = "http://www.example.com";
postData = "username="+URLEncoder.encode("my_username", "UTF-8")+"&password="+URLEncoder.encode("my_password", "UTF-8");
 webview.postUrl(url,postData.getBytes());