Volley Not making request on latest version of Android

前端 未结 4 634
旧巷少年郎
旧巷少年郎 2020-12-10 08:59

I noticed my app wasn\'t working on Pixel 3. I went into android studio and emulated a few devices and noticed it is not working on any Android Pie (API 28) devices, but wor

相关标签:
4条回答
  • 2020-12-10 09:24

    For this issue i use https and its working very well now

    0 讨论(0)
  • 2020-12-10 09:25

    This is because starting with Android P, any network traffic between your app and insecure destinations must be explicitly whitelisted. See Protecting users with TLS by default in Android P.

    In your code, you're making a request to:

    http://hellapunk.com/listallshows.php?id=2018
    

    The http:// shows that the site is not secure. If you dig deeper into your LogCat you'll likely find a message such as:

    com.android.volley.NoConnectionError: java.io.IOException: Cleartext HTTP traffic to hellapunk.com not permitted
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:177)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
     Caused by: java.io.IOException: Cleartext HTTP traffic to hellapunk.com not permitted
        at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)
        at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:99)
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:131)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120) 
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87) 
    

    Attempting it myself, it doesn't appear that the site supports https connections, so in order to reach this particular server from your application, you would need to whitelist the hellapunk.com domain manually.

    In your resources directory, define an XML document for your network security configuration (e.g. res/xml/network_security_config.xml):

    network_security_config.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">hellapunk.com</domain>
        </domain-config>
    </network-security-config>
    

    Then, in your AndroidManifest.xml for your application, in the <application> tag, add the attribute:

    <application
        android:networkSecurityConfig="@xml/network_security_config"
    

    You should then be allowed to make insecure requests to any domain specified within that file.

    0 讨论(0)
  • 2020-12-10 09:29

    Or you can use usesCleartextTraffic attribute under the application element in the android manifest. The default value in Android P is “false”.

    <application
         android:usesCleartextTraffic="true"
    
    </application>
    

    but it requires minSdkVersion 23 or higher.

    0 讨论(0)
  • 2020-12-10 09:36

    It is working well in Pie Api 28

    network_security_config.xml:

    <?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>
    

    AndroidManifest.xml

    In the android manifest Application Tag, add these lines.

     android:networkSecurityConfig="@xml/network_security_config"
     android:usesCleartextTraffic="true"
    
    0 讨论(0)
提交回复
热议问题