我有一个来自第三方的网址。我需要加载这个url,在用户输入密码后,它应该向android客户端发送一个令牌。网址如下所示:
https://api.sandbox.veritrans.co.id/v2/token/redirect/521111-1117-bad66da4-cc24-4c59-9ef6-05641fc82c60
我在webview
打开网址。我的webview fragment
看起来像这样:
public class WebView3DSFragment extends Fragment {
@BindView(R.id.webview_3ds)
WebView webView;
public static WebView3DSFragment newInstance(Bundle b) {
WebView3DSFragment fragment = new WebView3DSFragment();
fragment.setArguments(b);
return fragment;
}
@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_3ds_webview, container, false);
ButterKnife.bind(this, rootView);
String url = getArguments().getString("url", "");
if (url != null) {
webView.getSettings().setJavaScriptEnabled(true);
injectJavaScript();
webView.loadUrl(url);
}
return rootView;
}
public void injectJavaScript(){
webView.addJavascriptInterface(new JS3DSInterface(), "Android");
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.loadUrl(
"javascript:(function callback(response) {\n" +
" if (response.redirect_url) {\n" +
" // If 3Dsecure transaction. Open 3Dsecure dialog\n" +
" console.log('Open Dialog 3Dsecure');\n" +
" openDialog(response.redirect_url);\n" +
"\n" +
" } else if (response.status_code == '200') {\n" +
" // success 3d secure or success normal\n" +
" //close 3d secure dialog if any\n" +
" closeDialog();\n" +
"\n" +
" // store token data in input #token_id\n" +
" $(\"#token-id\").val(response.token_id);\n" +
" " +"Android.set3DSToken(response.status_message)"+
"\n" +
" } else {\n" +
" // failed request token\n" +
" //close 3d secure dialog if any\n" +
" closeDialog();\n" +
" $('#submit-button').removeAttr('disabled');\n" +
" // Show status message.\n" +
" $('#message').text(response.status_message);\n" +
"console.log(JSON.stringify(response));\n" +
" }\n" +
" }\n" +
"\n" +
" // Open 3DSecure dialog box\n" +
" function openDialog(url) {\n" +
" // make sure to load fancybox in a script tag\n" +
" $.fancybox.open({\n" +
" href: url,\n" +
" type: 'iframe',\n" +
" autoSize: false,\n" +
" width: 400,\n" +
" height: 420,\n" +
" closeBtn: false,\n" +
" modal: true\n" +
" });\n" +
" }\n" +
"\n" +
" // Close 3DSecure dialog box\n" +
" function closeDialog() {\n" +
" $.fancybox.close();\n" +
" }\n" +
"});");
}
});
}
class JS3DSInterface {
@JavascriptInterface
public String set3DSToken(String s) {
return "Java method called!!"+s;
}
}
}
我在代码中的javascript
实际上是在这里找到的:
http://api-docs.midtrans.com/#get-token
我觉得如果我能够将javascript
注入webview
,那么我可以在用户输入信息时获得回调。但是当我运行webview时,url会加载,但是当我在chrome中检查stetho时://检查我放入的webview
不包含javascript
。如何注入javascript
?< / p>
更新:即使无法注入以下内容:
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:alert('called by Android')");
}
}
因为我从未看到过显示的警报。
答案 0 :(得分:1)
在WebView中加载javascript
webView.getSettings()。setDomStorageEnabled(true);