zoukankan      html  css  js  c++  java
  • java.io.IOException: Cleartext HTTP traffic to xxx.xxx.xxx.xxx not permitted

    java.io.IOException: Cleartext HTTP traffic to xxx.xxx.xxx.xxx not permitted

    Android9.0 默认是禁止所有的http

    请求的,需要在代码中设置如下代码才可以正常进行网络请求: android:usesCleartextTraffic="true"。如图

    1.  
      <application
    2.  
      android:name="xxxx.xxxx.xxx.xxx"
    3.  
      android:allowBackup="true"
    4.  
      android:icon="@drawable/ic_launcher"
    5.  
      android:label="@string/app_name"
    6.  
      android:theme="@style/AppBaseTheme"
    7.  
      android:usesCleartextTraffic="true">
     
    其它:

    Android高版本联网失败报错:Cleartext HTTP traffic to xxx not permitted解决方法

    转载https://blog.csdn.net/gengkui9897/article/details/82863966

    前言:为保证用户数据和设备的安全,Google针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的连接,因此运行 Android P 系统的安卓设备无论是接收或者发送流量,未来都不能明码传输,需要使用下一代(Transport Layer Security)传输层安全协议,而 Android Nougat 和 Oreo 则不受影响。

    因此在Android P 使用HttpUrlConnection进行http请求会出现以下异常

     W/System.err: java.io.IOException: Cleartext HTTP traffic to **** not permitted

    使用OKHttp请求则出现

    java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by network security policy

    在Android P系统的设备上,如果应用使用的是非加密的明文流量的http网络请求,则会导致该应用无法进行网络请求,https则不会受影响,同样地,如果应用嵌套了webview,webview也只能使用https请求。

    针对这个问题,有以下三种解决方法:

    (1)APP改用https请求

    (2)targetSdkVersion 降到27以下

    (3)更改网络安全配置

    前面两个方法容易理解和实现,具体说说第三种方法,更改网络安全配置。

    1.在res文件夹下创建一个xml文件夹,然后创建一个network_security_config.xml文件,文件内容如下:

    1.  
      <?xml version="1.0" encoding="utf-8"?>
    2.  
      <network-security-config>
    3.  
      <base-config cleartextTrafficPermitted="true" />
    4.  
      </network-security-config>

    2.接着,在AndroidManifest.xml文件下的application标签增加以下属性:

    1.  
      <application
    2.  
      ...
    3.  
      android:networkSecurityConfig="@xml/network_security_config"
    4.  
      ...
    5.  
      />

    完成,这个时候App就可以访问网络了。

     
  • 相关阅读:
    guxh的python笔记一:数据类型
    guxh的python笔记三:装饰器
    guxh的python笔记十:包和模块
    guxh的python笔记六:类的属性
    guxh的python笔记四:迭代
    流畅的python笔记
    spring面试大全
    Spring面试问答
    Hibernate的10个常见面试问题及答案
    reflect 机制
  • 原文地址:https://www.cnblogs.com/it-tsz/p/10810756.html
Copyright © 2011-2022 走看看