Android implements WebView to load html files under the assets folder, android embeds h5 for interaction, and debugs errors net :: ERR_FILE_NOT_FOUND

Debug error net :: ERR_FILE_NOT_FOUND 

My environment: win7, Android studio 3.0.1. Java8;

webView.loadUrl("file:////android_asset/text");

Use of html hyperlink in Android studio:

<a href="file:///android_asset/text2">点击</a></head>

Interaction between Android and H5


1. The webView loading page
We all know that in Android, the webview is used to load the html page, and the writing is different according to the location of the HTML file:
// For example: load the test.html page under the assets folder

webView.loadUrl("file:////android_asset/text");

: Load webpage

mWebView.loadUrl("http://www.baidu.com")


If you just call mWebView.loadUrl () to load it, then when you click on the link in the page, the page will open in your phone's default browser. If you want the page to open in the App, then you must set setWebViewClient:

mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    mWebView.loadUrl(url);
                    return true;
                }
            }


2. Android locally calls JavaScript methods in HTML pages through Java.
If you want to call js methods, you must make webView support
WebSettings webSettings = mWebView.getSettings ();
// Set to call js method
webSettings.setJavaScriptEnabled (true);

if called The js method has no return value, you can directly call mWebView.loadUrl ("JavaScript: do ()"); where do is a method in js; if there is a return value, we can call the mWebView.evaluateJavascript () method:

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.evaluateJavascript("sum(1,2)", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        tv.setText("sum= "+value);
                    }
                });
            }
        });


The js code is as follows:

<script type="text/javascript">
function sum(a,b){
return a+b;
}

</script>


2. js calls Android native Java method
In Android4.2 or above, you can directly use @JavascriptInterface annotation to declare, the following is a local Java method

package com.itep.myapplication;

import android.webkit.JavascriptInterface;

/**
 * Created by Administrator on 2020/4/10 0010.
 */

public class JsInteration {
    @JavascriptInterface
    public String back() {
        return "hello world";
    }

}


After defining this method, call the mWebView.addJavascriptInterface () method:

webView.addJavascriptInterface(new JsInteration(), "android_ceshi");


So how to call it in js?

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <h1>text 2</h1>
    <button οnclick="s()">dianji</button>
    <script type="text/javascript">
    function sum(a,b){
    return a+b;
    }
     function s(){
    var result =window.android_ceshi.back();
    document.getElementById("p").innerHTML=result;
    }
     function ceshi(){

   var ss="123";
   document.write(ss);
    }


    </script>
</head>
<body>
<button οnclick="s()">调用本地方法</button>
<a href="file:///android_asset/test2">点击</a>
<p id="p"></p>
</body>
</html>



4. Intercept click events in HTML pages

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//判断url拦截事件
                if (url.equals("file:///android_asset/text2")) {
                    Log.e(TAG, "shouldOverrideUrlLoading: " + url);
                    startActivity(new Intent(MainActivity.this,MainActivity_two.class));
                    return true;
                } else {
                    webView.loadUrl(url);
                    return false;
                }
            }
        });


 

tex.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <p>你好你好script type你好script type你好script type你好script type</p>
    <button οnclick="ceshi()">dianji</button>
    <button οnclick="ceshi()">dianji</button>
    <button οnclick="ceshi()">dianji</button>
    <button οnclick="ceshi()">dianji</button>
    <button οnclick="ceshi()">dianji</button>
    <button οnclick="ceshi()">dianji</button>
    <script type="text/javascript">
    function sum(a,b){
    return a+b;
    }
     function s(){
    var result =window.android.back();
    document.getElementById("p").innerHTML=result;
    }
     function ceshi(){
     var ss="123";
     document.write(ss);
    }


    </script>
    <a href="file:///android_asset/text2">点击</a></head>
<body>
<button οnclick="s()">调用本地方法</button>

<p id="p">ceshi</p>
</body>
</html>

text2.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <h1>text 2</h1>
    <button οnclick="s()">dianji</button>
    <script type="text/javascript">
    function sum(a,b){
    return a+b;
    }
     function s(){
    var result =window.android.back();
    document.getElementById("p").innerHTML=result;
    }
     function ceshi(){

   var ss="123";
   document.write(ss);
    }


    </script>
</head>
<body>
<button οnclick="s()">调用本地方法</button>
<a href="file:///android_asset/test2">点击</a>
<p id="p"></p>
</body>
</html>

 

MainActivity.class

package com.itep.myapplication;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    WebView webView;
    TextView tv;

    /**
     * @param savedInstanceState
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        webView = findViewById(R.id.wb);
        WebSettings webSettings = webView.getSettings();
        //设置为可调用js方法
        webSettings.setJavaScriptEnabled(true);
        webView.getSettings().setDefaultTextEncodingName("utf-8");
//        webView.loadUrl("file:////android_asset/text.html");
        webView.loadUrl("file:////android_asset/text");
        // Example of a call to a native method
        tv = (TextView) findViewById(R.id.sample_text);
        webView.addJavascriptInterface(new JsInteration(), "android");
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//判断url拦截事件
                if (url.equals("file:///android_asset/text2")) {
                    Log.e(TAG, "shouldOverrideUrlLoading: " + url);
                    startActivity(new Intent(MainActivity.this,MainActivity_two.class));
                    return true;
                } else {
                    webView.loadUrl(url);
                    return false;
                }
            }
        });
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        //        tv.setText(new Text().stringFromJNI());
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

 

JsInteration:
package com.itep.myapplication;

import android.webkit.JavascriptInterface;

/**
 * Created by Administrator on 2020/4/10 0010.
 */

public class JsInteration {
    @JavascriptInterface
    public String back() {
        return "hello world";
    }

}

 

Published 649 original articles · praised 659 · 590,000 views

Guess you like

Origin blog.csdn.net/qq_38998213/article/details/105438878