0

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> 
2
  • Could you please explain bit more with your analysis. Commented Nov 26, 2019 at 10:58
  • @RajPaliwal i want to get resPG value from my apps in webview . resPG load value and i want to get the value . Commented Nov 26, 2019 at 11:01

2 Answers 2

1

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;?>'};)"); } 
Sign up to request clarification or add additional context in comments.

6 Comments

getvalues(value) ? value
getvalues is your javascript interface function
**Not Working **
check now and revert
getting null value
|
1

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 } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.