Search code examples
androidandroid-webview

How can I fix this android WebView problem?


I have add <uses-permission android:name="android.permission.INTERNET"></uses-permission> to 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>

This is 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);
    }

}

And I have done some research on this problem...

so I also have put this network_security_config.xml file in 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>

but when I testing the app, it just working on version 7.1.1 (API 25)...

else, if i testing on android 8/9 version, it would be showing an error page or white screen

could anyone help?


Solution

  • Android P requires HTTPS by default. What this means is that if you are using unencrypted HTTP requests in your app, the app will work fine in all lower versions than Android P.

    To avoid this security exception, try below changes in your network_security_config.xml file.

    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>