WebView is not loading page in Android 9.0?

后端 未结 7 1631
时光说笑
时光说笑 2020-12-03 05:24
 public abstract class MainActivity extends AppCompatActivity {

        private static WebView web;
        private WebView mWebView;
        private java.lang.Stri         


        
相关标签:
7条回答
  • 2020-12-03 05:50
         //  Step 1:    in manifest :
      <application
            android:networkSecurityConfig="@xml/cleartextTrafficPermitted">
        //   Step 2:       
                      <uses-library
                             android:name="org.apache.http.legacy"
                            android:required="false" />
      </application>
    
        // Step 3 Create drawable XML>>cleartextTrafficPermitted  :
    
          <?xml version="1.0" encoding="utf-8"?>
            <network-security-config>
               <base-config cleartextTrafficPermitted="true">
               <trust-anchors>
                  <certificates src="system" />
               </trust-anchors>
               </base-config>
            </network-security-config>
    
    0 讨论(0)
  • 2020-12-03 05:54

    Same issue occurred in my project but all of the above answers not worked for me. so after wasting a whole day on this issue,what i got is

    In Android version 8.1/9/10 if your are using wrap_content attribute for height in WebView ,then it will always set to 0dp. In earlier versions(below 8.1) wrap_content in WebView works fine. but from 8.1 and above you can give height either statically or you can set it dynamically through program using setLayoutParameters(). And also add extra attribute to Application ( android:usesCleartextTraffic="true" ) in android manifest.

    0 讨论(0)
  • 2020-12-03 05:56

    This method works for all domains also with Android 9. Add this property to your Manifest like this:

    <application
        ...
       android:usesCleartextTraffic="true"
        ...>
    </application>
    
    0 讨论(0)
  • 2020-12-03 05:59

    In android 10 finally, this is worked for me to load full content rather partial.

    webView.loadDataWithBaseURL(null, contentText, "text/html", "UTF-8", null);
    
    0 讨论(0)
  • 2020-12-03 06:10

    Please try to use secure url. Use https instead of http. Android 9.0 don't allow unsecured urls

    0 讨论(0)
  • 2020-12-03 06:11

    Actually you should avoid using http, but if there is no way you can do this:

    1. Add @xml/network_security_config into your resources:

      <?xml version="1.0" encoding="utf-8"?>
      <network-security-config>
          <domain-config cleartextTrafficPermitted="true">
              <domain includeSubdomains="true">www.smedk.ru</domain>
          </domain-config>
      </network-security-config>
      
    2. Add this security config to your Manifest like this:

      <application
          ...
          android:networkSecurityConfig="@xml/network_security_config"
          ...>
      
          ...
      </application>
      
    3. Now you allowed using HTTP connection on www.smedk.ru subdomains.

    You can read more in https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted

    Note: The guidance in this section applies only to apps that target Android 8.1 (API level 27) or lower. Starting with Android 9 (API level 28), cleartext support is disabled by default.

    0 讨论(0)
提交回复
热议问题