0

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?

14
  • What is the URL? Sep 19, 2019 at 4:01
  • What specifically is the problem?
    – JoxTraex
    Sep 19, 2019 at 4:03
  • You just need to pass domain in network_security_config.xml file. e.g. <domain includeSubdomains="true">www.smarterasp.net</domain> So remove 'http://' from that.
    – DHAVAL A.
    Sep 19, 2019 at 4:09
  • @RahulKhurana the URL is cloud hosting by SmarterASP.NET, and the service is using HTTP
    – redPanda
    Sep 19, 2019 at 4:20
  • Try giving android:usesCleartextTraffic="true" to the application tag and remove the android:networkSecurityConfig="@xml/network_security_config" line Sep 19, 2019 at 4:22

2 Answers 2

0

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>
2
  • Thank you! But now the error page/ blank page change to white screen... there's no anything else to show, what can I do?
    – redPanda
    Sep 19, 2019 at 6:33
  • Please wait for some times, may be your website taking time to load views. Because I have checked your code with my emulator. And it is working fine.
    – DHAVAL A.
    Sep 19, 2019 at 6:56
0

Try this code to resolve this error : Change http://www.smarterasp.net to 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>

Manifest

add These two lines

<application
      ..
        android:networkSecurityConfig="@xml/network_security_config"
        android:resizeableActivity="false"
       ..>
1
  • I see your point, thank you! But now the page change to display a white screen... do you know what is going on?
    – redPanda
    Sep 19, 2019 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.