简体   繁体   English

webview中的本地页面未显示

[英]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适用于https站点,但不适用于位于/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 以下是test.html文件

<!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 . 现在,我想使用webView加载此页面。

Below is the mainactivity.java code 下面是mainactivity.java代码

    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 : 下面是我的WebInterface java类:

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 . 现在,我不明白为什么它不加载webView.loadUrl中提到的网页。 Note: I having given internet permissions in manifest file. 注意:我在清单文件中赋予了Internet权限。

So if the file is local in the device (android phone), you need to have the path of the file. 因此,如果文件在设备(Android手机)中是本地文件,则需要具有文件的路径。 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 这是有关如何访问资源的文档: 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. 如果您所说的本地计算机在您的计算机上,并且您正在托管文件(我认为这只是为了测试),那么您需要将android设备/仿真器连接到与计算机相同的本地网络,然后通过您计算机的本地IP。

And here is another question similar to this that has already been answered: Load HTML file into WebView 这是与该问题类似的另一个问题:将HTML文件加载到WebView中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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