Rectangle 27 7

First of all, I think it's unlikely that you really want to be accessing HTML and JavaScript files from the assets, as the assets cannot (and shouldn't) be updated once the app has been packaged and signed. This means, that with your HTML and JavaScript in the assets, you cannot update them. The approach to solve that problem is to save the HTML and JavaScript in the internal or the external storage, and use it from there.

Secondly, if you use that approach I just explained, you cannot any longer use the WebView.loadUrl() method, but instead you will need to use the WebView.loadDataWithBaseUrl() method. Like this:

webView.loadDataWithBaseURL("http://some_url/", "some_data", "text/html", "utf-8", null);

Then, "some_data" will be tried loaded as an HTML page, as seen in this previous SO answer. When it tries to load a JavaScript resource, like /scripts/lib.js, it will be resolved to http://some_url/scripts/lib.js, and the WebView will try to load that resource. Of course it cannot, and that is why you (in Java) will need to extend the WebViewClient class, and provide an implementation for the shouldInterceptRequest(WebView wv, String url) method.

When any resource is tried to be fetched in the web page, shouldInterceptRequest will be called, and its return value used as the resource.

android - webview to load the local html file which contains .js as android

Rectangle 27 0

I'm having the same problem! There is another indirect way. You get the HttpResponse and then save the html file to the internal storage and then load the file into the WebView. I never tried this before, though.

Android WebView - NTLM Authentication - Stack Overflow

android webview android-webview
Rectangle 27 0

You can use a Javascript interface for the WebView to return the entirety of the HTML source when the page is finished loading. To do this, you'll need to assign your own WebViewClient to the WebView.

To do this, use something similar to the following in your Activity class -- Make sure your Activity implements Observer:

public void onCreate(Bundle savedInstanceState) {
    // ...

    webView.setWebViewClient(new MyWebViewClient());
    HtmlJSInterface htmlJSInterface = new HtmlJSInterface();
    webView.addJavascriptInterface(htmlJSInterface, "HTMLOUT");
    htmlJSInterface.addObserver(this);

    // ...
}

// Called when our JavaScript Interface Observables are updated.
@Override
public void update(Observable observable, Object observation) {

    // Got full page source.
    if (observable instanceof HtmlJSInterface) {
        html = (String) observation;
        onHtmlChanged();
    }
}

private void onHtmlChanged() {
    // Do stuff here...
}

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // When each page is finished we're going to inject our custom
        // JavaScript which allows us to
        // communicate to the JS Interfaces. Responsible for sending full
        // HTML over to the
        // HtmlJSInterface...
        isStarted = false;
        isLoaded = true;
        timeoutTimer.cancel();
        view.loadUrl("javascript:(function() { "
                + "window.HTMLOUT.setHtml('<html>'+"
                + "document.getElementsByTagName('html')[0].innerHTML+'</html>');})();");
        }
    }
}

Then, you're going to want to create the HtmlJSInterface class, as such:

public class HtmlJSInterface extends Observable {
  private String html;

  /**
   * @return The most recent HTML received by the interface
   */
  public String getHtml() {
    return this.html;
  }

  /**
   * Sets most recent HTML and notifies observers.
   * 
   * @param html
   *          The full HTML of a page
   */
  public void setHtml(String html) {
    this.html = html;
    setChanged();
    notifyObservers(html);
  }
}

By the way, please ignore the extra variables and Timer in onPageFinished(). :)

Thanks for the answer. But, where is this html file is going to be saved in the internal storage?

html - Save webview content to android storage and load it - Stack Ove...

android html webview storage
Rectangle 27 0

Check for the internet first (You can find many resolved question on S.O.), If there is internet connection, use HttpClient to get HTML source and save it to external storage. and Then load the webpage from external storage in webview. If no internet connection, simply load page in webview.

HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
  HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do
 HttpResponse response = httpclient.execute(httpget); // Executeit
 HttpEntity entity = response.getEntity(); 
 InputStream is = entity.getContent(); // Create an InputStream with the response
  BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
   String line = null;
   while ((line = reader.readLine()) != null) // Read line by line
        sb.append(line + "\n");
    String resString = sb.toString(); // Result is here
     is.close(); // Close the stream
    File file = new File(Environment.getExternalStorageDirectory().toString()+"/Path/to/save/file/index.html");
        file.createNewFile();
        FileOutputStream f1 = new FileOutputStream(file, false);
        PrintStream p = new PrintStream(f1);
        p.print(resString);
        p.close();
        f1.close();

    }catch(IOException e){}

Edit : As @michaelcarrano says in comment, perform this work in background by using another thread.

Its gonna work for sure. My app also rely on samething but instead of using webview I directly display useful data! :)

Be sure to do the downloading off the UI thread.

java - What would be the easiest way to display a certain page in WebV...

java android eclipse