WebViewer Can't Access and Load HTML File (in standard emulator)

I'm following this tutorial: https://appinventor-mit-edu.ezproxy.canberra.edu.au/explore/ai2/webview-javascript

This is my block code:

This is the error I get (I'm using the emulator on Windows 10 with aiStarter):
image

I've tried alternative paths like http://localhost/javascriptWebViewProcessor.html, but it doesn't work.

javascriptWebViewProcessor.html:

<!doctype html>
<head>
<title>WebView Javascript Processor</title>
</head>

<body onload="processJavascript();">
<b>This page includes a javascript function that reverses text.</b>
<p>The javascript function incorporates a special App Inventor feature called <i>window.AppInventor.getWebViewString()</i>, which allows App Inventor apps to communicate with the WebViewer component's internal processing of javascript.
<p>This simple example shows how to use the <i>window.AppInventor.getWebViewString()</i> function to pass data to and from the WebViewer component, and thereby an App Inventor app.

<script>
    var result = new Date().toString();
    var appInventorInput = window.AppInventor.getWebViewString();
        function processJavascript() {
        if (appInventorInput.length > 0) {
            document.write( "WebView InputString received FROM app:" + appInventorInput );
            result = appInventorInput.split("").reverse().join("");
            document.write( "<p/>WebView InputString sending BACK TO app:<br/>" +
            result );
        } else {
            document.write( "No WebView InputString set by app at: <br/>" + result );
        }
        window.AppInventor.setWebViewString( result );
    }
</script>

</body>
</html>

I tried looking through the forums and while I have found threads with this issue, they always point back to the localhost solution or put the HTML file in an actual server so I don't have to access it locally.

I guess my question is what am I doing wrong and what is the path to these Media files:

image

Not sure if the emulator can cope...

http://localhost/your.html

should work in companion and compiled projects

Try with the attached simpler html file in your assets. Does this display in your webviewer?
(you will need to remove the .txt from the end of the filename)

hello.html.txt (223 Bytes)

<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<head>
<title>demoPage</title>
</head>
<body>
<h1>Hello World</h1>	
</body>
</html>
http://localhost/hello.html

image

I generally use the goToUrl block, and do not use the HomeUrl or GoHome blocks

Should have asked at the beginning - Android or iOS ? but given the use of emulator i am guessing Android.

1 Like

Yes I'm using Android.

Simpler html file doesn't work either:
image

Try to use file tools extension by @Taifun.
Use the block join with the method path to asset and hello.html

Where can I find that?

May be that the "document.write(...)" statement is the cause because the use of ".write()" overwrites the entire HTML code of the document the "<script>" included so that its code, your script, cannot be executed.

1 Like

Can't be that, I tried using a simpler HTML file and it still didn't work. I believe it has something to do with accessing files while on the companion emulator

1 Like

I'll check out the File Extension by Taifun, but I'd like to point attention to reasons why WebViewer isn't rendering the HTML file?

image

What's the Android version of your phone?

Is this it?

System Requirements

To use App Inventor, your computer must meet the following system requirements:

Computer and operating system

  • Macintosh (with Intel processor): Mac OS X 10.5, 10.6+
  • Windows: Windows XP, Windows Vista, Windows 7+
  • GNU/Linux: Ubuntu 8+, Debian 5+ (Note: GNU/Linux live development is only supported for WiFi connections between computer and Android device.)

Browser

  • Mozilla Firefox 3.6 or higher (Note: If you are using Firefox with the NoScript extension, you'll need to turn the extension off. See the note on the troubleshooting page.)
  • Apple Safari 5.0 or higher
  • Google Chrome 4.0 or higher
  • App Inventor does not support Microsoft Internet Explorer. Windows users should use Chrome or Firefox

Phone or Tablet (or use the on-screen emulator)

  • Android Operating System 2.3 ("Gingerbread") or higher

https://appinventor-mit-edu.ezproxy.canberra.edu.au/explore/content/system-requirements.html

try file:///android_asset/hello.html

1 Like

I should've done this earlier, but the WebView component does work with other websites like http://google.com:

image

So now it's just figuring out why it's not rendering HTML files that I've uploaded to Media (in other words why it's not rendering HTML files stored locally)

Didn't work btw to try "file:///android_asset/hello.html"

image

Can you send a .aia file with that project of yours so I can test it on my end? Maybe there is something wrong with the way I set up aiStarter and the emulator

Instead of using the emulator, I went ahead and tested on iOS using the AI Companion app. The page still doesn't load. Also I don't think accessing the file is the main issue because according to the changelogs, http://localhost/ should work:

Here is what it looks like on iOS:

I've also tested using http://google.com and WebViewer was able to successfully render it:

I've found an old post that apparently updates the old tutorial I was referencing. I followed it exactly and it still doesn't work. I keep getting the "Could not connect to the server" error. I'm including the project I've created. If somebody doesn't mind, please import the project and test it for yourself.

SimpleWebviewerJavascriptProcessor.aia (2.9 KB)

Your project for me works fine testing with companion.

1 Like

Android or iOS? I guess it doesn't really matter since on my end I tested using both Android emulator and iOS companion app on my own phone, and neither worked. Honestly I'm even more confused why it works for others but not for me.