Cross-platform asynchronous invocation between Android and JS
Why the sudden need to do this problem?
It took nearly 1 days to figure out the solution to the problem of dog blood when developing a browser, and Android and Javascirpt were intermodulation.
Because the interface is crawled by someone else, so there is a JS cross-domain problem, Android shiny debut buttoned up.
GIF Animation demo
WebView Related Settings
WebSettings mwebsettings = GetSettings ();
Mwebsettings.setdefaulttextencodingname ("UTF-8");/Set the default display encoding
mwebsettings.setjavascriptenabled (TRUE); Call the JS method. Android version is greater than 17 plus annotation @JavascriptInterface
Directly enlarge recruit-> paste Code
Android
Addjavascriptinterface (New Object () {@JavascriptInterface public void toastmessage (final String URL, final i
NT type, final int dir) {l.e ("url =" + URL + "type =" + Type + "DIR =" + dir); Apiwrapper.getinstance (). Getlenovoword (URL). Subscribeon (Schedulers.io ()). Observeon (
Androidschedulers.mainthread ()). Subscribe (New rxsubscriber<responsebody> () {@Override public void _onnext (Responsebody responsebody) {try {String data = Respons
Ebody.string ();
L.E ("data =" + data);
Loadurl ("javascript:rescompleted + data +", "+ Type +", "+ dir +"));
catch (IOException e) {e.printstacktrace (); @Override public void _OnError (String msg) {Loadurl ("Javas
cript:rescompleted ("+ msg +")); }
}); }, "Android");
Html
Thank you for reading, I hope to help you, thank you for your support for this site!