MD. Anamul Hasan
MD. Anamul Hasan

Reputation: 23

How to get var from Android webview

I Want to get the value of resPG from webview after it load this page

<script>
$(document).ready(function(){
    var resPG = {status: '<?php echo $status;?>', msg: '<?php echo $msg;?>', trans_no: '<?php echo $Tran_ID;?>', pay_amt: '<?php echo $Amount;?>', pg_type: '<?php echo $Tran_Type;?>', billno: '<?php echo $RRN;?>', PAN: '<?php echo $PAN;?>'};   
    localStorage.setItem('resPG', JSON.stringify(resPG));
});
</script>

Upvotes: 0

Views: 828

Answers (2)

Damodhar
Damodhar

Reputation: 1317

you need to create a Javascript interface for get values from web to android native. for working example click this link

step1) Enable webview setting

WebView mWebView = findViewById(R.id.myWebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new MyWebChromeClient((Activity)mContext));
mWebView.addJavascriptInterface(new testClass(), "jsinterface");
mWebView.loadUrl("UrlToLoad");

create interface :

public class testClass{
    public testClass() {
    }

    @JavascriptInterface
    public void getvalues(value) {
       Log.i('web view value',value)
    }
}

step 3: add interface function inside JS

$(document).ready(function(){
    var resPG = {status: '<?php echo $status;?>', msg: '<?php echo $msg;?>', trans_no: '<?php echo $Tran_ID;?>', pay_amt: '<?php echo $Amount;?>', pg_type: '<?php echo $Tran_Type;?>', billno: '<?php echo $RRN;?>', PAN: '<?php echo $PAN;?>'};   
    localStorage.setItem('resPG', JSON.stringify(resPG));
jsinterface.getvalues(resPG );
});

another way is for better handling

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                webView.evaluateJavascript("{status: '<?php echo $status;?>', msg: '<?php echo $msg;?>', trans_no: '<?php echo $Tran_ID;?>', pay_amt: '<?php echo $Amount;?>', pg_type: '<?php echo $Tran_Type;?>', billno: '<?php echo $RRN;?>', PAN: '<?php echo $PAN;?>'};", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String s) {
                        contentView.setText(s);
                    }
                });
            }
            else {
                webView.loadUrl("javascript:window.jsinterface.getvalues({status: '<?php echo $status;?>', msg: '<?php echo $msg;?>', trans_no: '<?php echo $Tran_ID;?>', pay_amt: '<?php echo $Amount;?>', pg_type: '<?php echo $Tran_Type;?>', billno: '<?php echo $RRN;?>', PAN: '<?php echo $PAN;?>'};)");

            }

Upvotes: 1

Vinay Jayaram
Vinay Jayaram

Reputation: 1005

You can use JavaScript scheme in web-view load URL something like below

webview.loadUrl("javascript:Android.getMyData(MyData);");

//Add the javascript interface to your web view
this.addJavascriptInterface(new MyJavaScriptInterface(webViewContext), "Android");

public class MyJavaScriptInterface{
    Context mContext;

    /** Instantiate the interface and set the context */
    CustomJavaScriptInterface(Context c) {
        mContext = c;
    }


    /** retrieve the data */
    public void getMyData(final String myData) {

        //Do somethings with the Data

}

Upvotes: 1

Related Questions