I make a mobile application for the site.
The site is not mine, there is no API, so it has to be parsed.
I thought I would manage only JSOUP, but the question arose of how to handle javascript buttons.
After asking questions in an English-language stack, I was informed that I couldn’t process javascript with JSOUP.
I decided to do it through WebView, but now I ran into a problem - how to parse a page that is now displayed in a webview?
|
1 answer
DECISION:
We write handler WebView and we stick Handler there.
This Handler will tear up for us html pages immediately upon completion of the download.
I did something 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("Ваш Сайт"); }
Next, we write the class MyJavaScriptInterface
in which we MyJavaScriptInterface
the pulled out html page using 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); } }
Naturally, you also 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' //вот наша библиотека }
Everything is super! Now the title of my Activity has taken the value of the text in this id.
- Not quite clear what the
shit
? Where did they get it. - Roman Shubenko 4:27 pm
|
JSOUP
, for example.JSOUP
can take a string that you pull out from the WebView - Vladyslav Matviienko