Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the HTML code from WebView

Tags:

I would like to preemptively get the HTML code of a webpage that is to be loaded in a webView, parse it using regex, and display only the HTML code that I want, while letting the webpage still think it has loaded everything.

Is there any way to do that in the WebViewClient.onLoadResource() or similar methods?

EDIT: I tried this:

class MyJavaScriptInterface    {         @SuppressWarnings("unused")            public void showHTML(String html, Context context)            {               new AlertDialog.Builder(context)                    .setTitle("HTML")                    .setMessage(html)                    .setPositiveButton(android.R.string.ok, null)                .setCancelable(false)                .create();                  pageHTML = html;          }    }  @Override     public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {         mRom.setFileSize(getFileSize(mRom.getURLSuffix()));         webview.getSettings().setJavaScriptEnabled(true);         MyJavaScriptInterface interfaceA = new MyJavaScriptInterface();         webview.addJavascriptInterface(interfaceA, "HTMLOUT");           WebViewClient anchorWebViewClient = new WebViewClient()         {             @Override               public void onPageFinished(WebView view, String url)               {                   /* This call inject JavaScript into the page which just finished loading. */                   webview.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");                 Pattern pattern = Pattern.compile("<h2>Winning Sc.+</h2></div>(.+)<br>", Pattern.DOTALL);                 Matcher matcher = pattern.matcher(pageHTML);                 matcher.find(); 

The interface is never called

like image 806
Aymon Fournier Avatar asked Aug 13 '10 18:08

Aymon Fournier


People also ask

How do I display HTML content in WebView?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

Is it possible to get data from HTML forms into Android while WebView?

Yes you can, you can use javascript to get webpage content. Then use the webview jsInterface to return the content to you java code.


1 Answers

Had to use HttpClient. no cookies required, just parsing for html:

private String getDownloadButtonOnly(String url){     HttpGet pageGet = new HttpGet(url);      ResponseHandler<String> handler = new ResponseHandler<String>() {         public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {             HttpEntity entity = response.getEntity();             String html;               if (entity != null) {                 html = EntityUtils.toString(entity);                 return html;             } else {                 return null;             }         }     };      pageHTML = null;     try {         while (pageHTML==null){             pageHTML = client.execute(pageGet, handler);         }     } catch (ClientProtocolException e) {         // TODO Auto-generated catch block         e.printStackTrace();     } catch (IOException e) {         // TODO Auto-generated catch block         e.printStackTrace();     }          Pattern pattern = Pattern.compile("<h2>Direct Down.+?</h2>(</div>)*(.+?)<.+?>", Pattern.DOTALL);         Matcher matcher = pattern.matcher(pageHTML);         String displayHTML = null;         while(matcher.find()){             displayHTML = matcher.group();         }      return displayHTML; }      @Override     public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {         mRom.setFileSize(getFileSize(mRom.getURLSuffix()));         webview.getSettings().setJavaScriptEnabled(true);         WebViewClient anchorWebViewClient = new WebViewClient()         {              @Override             public void onPageStarted(WebView view, String url, Bitmap favicon) {                 super.onPageStarted(view, url, favicon);                 String downloadButtonHTML = getDownloadButtonOnly(url);                 if(downloadButtonHTML!=null && !url.equals(lastLoadedURL)){                     lastLoadedURL = url;                     webview.loadDataWithBaseURL(url, downloadButtonHTML, null, "utf-8", url);                 }             } 
like image 68
Aymon Fournier Avatar answered Oct 11 '22 11:10

Aymon Fournier