Note that there are some explanatory texts on larger screens.

plurals
  1. POAltering data shown in WebView
    primarykey
    data
    text
    <p>For one of my projects I am trying to display a page I fetch from the internet in a webview, but altering it before I do so. And I'm doing the same for all urls clicked from that webview.</p> <p>To accomplish this I've first made a class that extends WebView, and added a method <code>loadAlteredUrl(String url)</code></p> <pre><code> public void loadAlteredUrl(String url) { String page = getURLContent(url); String filtered; // Code for altering the webpage super.loadData(filtered, "text/html", "UTF-8"); //super.loadUrl("data:text/html;UTF-8," + filtered); } private String getURLContent(String url) { try{ HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet(url); HttpResponse response = httpClient.execute(httpGet, localContext); String result = ""; BufferedReader reader = new BufferedReader( new InputStreamReader( response.getEntity().getContent() ) ); String line = null; while ((line = reader.readLine()) != null){ result += line + "\n"; } return result; } catch (IOException ex) { return "IOEXCEPTION"; } </code></pre> <p>This code has been tested and works great. But now I could only alter my first page loaded, and this was not what I wanted. So I extended the WebViewClient class and overloaded the shouldOverloadUrl function:</p> <pre><code>public boolean shouldOverrideUrlLoading(WebView view, String url) { EnhancedWebview myview = (EnhancedWebview) view; if(!url.contains("http://")){ if(url.substring(0, 1).equals("/")) url = url.substring(1); myview.loadAlteredUrl(MainWindow.baseUrl + url); return true; } return false; } </code></pre> <p>The weird this is however, this code only works half of the time. When I debug this code, it always goes through the same steps, but for some weird reason, it will only open the url's if I actually doubleclick it or more. I have the feeling doubleclicking (or even spam clicking when necesarry) "breaks" something of the inner working which causes it to work correctly, because when I use it as it should be used, it goes through all the necessary steps, but doesn't load the page (even though the page is fetched using my custom methods etc).</p> <p>Anyone have any idea what im missing here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload