简体   繁体   中英

local page in webview not showing

Using webView.loadUrl is working for https site , but not with my local page which is located in /Applications/MAMP/htdocs/test.html

webView.loadUrl("/Applications/MAMP/htdocs/test.html") is not working
webView.loadUrl("http://localhost:8080/test.html"); is not working

Below is the test.html file

<!DOCTYPE html>
<html>
<head>
<title>Sample Page</title>
</head>
<body>

<h2>JS alert Example</h2>

<button onclick="myFunction()" >Try it</button>

<script>
function myFunction (){
    Android.showToast("Sample Android toast message");
}
</script>

</body>
</html>

Now I want to load this page using webView .

Below is the mainactivity.java code

    public class MainActivity extends AppCompatActivity {
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().hide();
        webView = (WebView) findViewById(R.id.webview);
        webView.loadUrl("http://127.0.0.1:8080/test.html");

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.addJavascriptInterface(new WebAppInterface (this), "Android");



    }
}

Below is my WebInterface java class :

public class WebAppInterface {

    private Context context;

    public WebAppInterface (Context context){
        this.context = context;
    }

    @JavascriptInterface
    public void showToast (String message){
        Toast.makeText(context,message,Toast.LENGTH_LONG).show();
    }
}

Now ,I cant understand why it is not loading the web page mentioned in webView.loadUrl . Note: I having given internet permissions in manifest file.

So if the file is local in the device (android phone), you need to have the path of the file. If the file is bundled in the assets, you can open the file like:

webView.loadUrl("file:///android_asset/filename.html");

Or if you can't find it, you can put the file in the raw resources and read the file into a string and then use:

webView.loadData(htmlString, "text/html", "utf-8");

In any case, most probably the problem you have is you can't find the file. So make sure you are placing the file in the proper place in the assets, or resources. And you are accessing the file correctly.

Here is the documentation on how to access the resources: https://developer.android.com/guide/topics/resources/providing-resources

If what you mean by local is on your computer, and you are hosting the file (which I assume would be just for testing), then you need to connect the android device / emulator to the same local network as your computer and then access via the local ip of your computer.

And here is another question similar to this that has already been answered: Load HTML file into WebView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM