How to pass the WebView data
-
I'm making a mobile application for the site.
Site's not mine, API isn't, so it has to be steamed.
I thought I'd only take JSOUP, but the question was how to handle javascript buttons.
After asking questions in the English-language pack, I was told that with JSOUP, I would not be able to process javascript.
I've decided to do the WebView, but now I''m in trouble-- how to evade the page that's now on the webview?
-
DECISION:
We're writing a WebView processor, and we're there, Handler.
This Handler will fight for us the html bruises at the end of the loading.
I've been doing it like this:String shit;
@JavascriptInterface
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);webview = (WebView) findViewById(R.id.webView); webview.setWebViewClient(new MyWebViewClient()); webview.getSettings().setJavaScriptEnabled(true); webview.getSettings().setDomStorageEnabled(true); webview.getSettings().setSaveFormData(true); webview.getSettings().setSavePassword(true); webview.getSettings().setLoadsImagesAutomatically(false); webview.addJavascriptInterface(new MyJavaScriptInterface(), "HtmlHandler"); webview.setWebViewClient(new MyWebViewClient() { @Override public void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:window.HtmlHandler.handleHtml" + "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');"); } }); webview.getSettings().setUserAgentString("Chrome/41.0.2228.0 Safari/537.36"); webview.loadUrl("Ваш Сайт");
}
Then write class.
MyJavaScriptInterface
In which we will dissolve the html page with JSOUP:private class MyJavaScriptInterface {
@JavascriptInterface
public void handleHtml(String html) {Document doc = Jsoup.parse(html); shit = doc.select("span[id=j_id178]").first().text(); MainActivity.this.setTitle(shit); }
}
Of course, there's still a need to connect the JSOUP library in the build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'org.jsoup:jsoup:1.8.3' //вот наша библиотека
}
That's great! Now the title of my Activity has taken the meaning of the text in this id.