如何解决这个Android WebView问题?

时间:2019-09-19 03:57:46

标签: android android-webview

我已将<uses-permission android:name="android.permission.INTERNET"></uses-permission>添加到AndroidManifest.xml.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app"
    >
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!--<uses-library android:name="org.apache.http.legacy" android:required="false"/>-->
        <activity android:name=".MainActivity" android:usesCleartextTraffic="true">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

这是MainActivity.java

import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        WebView webView = findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setDomStorageEnabled(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBlockNetworkImage(false);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setBuiltInZoomControls(true);

        Activity activity = this;
        mProgressDialog = ProgressDialog.show(activity, "Loading", "Welcome", true);
        mProgressDialog.setCancelable(false);

        WebViewClient client = new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
                mProgressDialog.show();

                webView.loadUrl(url);
                return true;
            }
            @Override
            public void onPageFinished(WebView view, final String url) {
                mProgressDialog.dismiss();
            }

            public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl){
                webView.loadUrl("file:///android_asset/error.html");
            }

        };
        webView.setWebViewClient(client);
        webView.loadUrl("http://xxxxxx.com/#/"); //Sorry i cant tell you :(

        setContentView(webView);
    }

}

我已经对此问题进行了一些研究...

所以我也将此network_security_config.xml文件放在了res/xml/

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <!--<domain includeSubdomains="true">http://xxxxxxxx.com</domain>-->
        <domain includeSubdomains="true">http://www.smarterasp.net</domain>

    </domain-config>
</network-security-config>

但是当我测试该应用程序时,它仅适用于版本7.1.1(API 25)...

否则,如果我在android 8/9版本上进行测试,它将显示错误页面或白屏

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

尝试使用此代码解决此错误:将 http://www.smarterasp.net 更改为 smarterasp.net

  

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">

        <domain includeSubdomains="true">youtube.com</domain>
       <domain includeSubdomains="true">smarterasp.net</domain>
    </domain-config>
</network-security-config>
  

清单

添加这两行

<application
      ..
        android:networkSecurityConfig="@xml/network_security_config"
        android:resizeableActivity="false"
       ..>

答案 1 :(得分:0)

Android P默认情况下需要HTTPS。这意味着,如果您在应用中使用未加密的HTTP请求,则该应用在所有低于Android P的版本中均可正常运行。

为避免此安全异常,请尝试在network_security_config.xml文件中进行以下更改。

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        //  Add host of your download URL in below line. 
        //   ie. if url is  "https://www.google.com/search?source=...."
        //   then just add "www.google.com"
        <domain includeSubdomains="true">www.smarterasp.net</domain>
    </domain-config>
</network-security-config>