【问题标题】:Android studio- No Network Security Config specifiedAndroid studio - 未指定网络安全配置
【发布时间】:2020-07-27 14:50:16
【问题描述】:

我目前正在 android studio 学习如何通过 Rob Percival 的 udemy 课程构建应用程序。

刚到了我想从互联网上获取信息的地步,我按照说明进行操作,但没有得到我期望的日志(来自网站的信息)。

出错了:

未指定网络安全配置,使用平台默认值

public class ImageDownloader extends AsyncTask<String,Void,String> {

    @Override
    protected String doInBackground(String... urls) {

        String result="";
        URL url;
        HttpURLConnection urlConnection= null;

        try {
            url = new URL(urls[0]);

            urlConnection=(HttpURLConnection)url.openConnection();

            InputStream in=urlConnection.getInputStream();

            InputStreamReader reader=new InputStreamReader(in);

            int data=reader.read();

            while (data != -1){
                char current= (char) data;

                result += result;

                data=reader.read();

            }
            return result;
        }

        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageDownloader task=new ImageDownloader();

    String result=null;


    try {
        result=task.execute("http://www.posh24.se/").get();
        Log.i("content url", result);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

我也将此行添加到Manifest.xml使用权限 android:name="android.permission.INTERNET"

我该怎么办?

【问题讨论】:

标签: java android


【解决方案1】:

您收到的消息不是错误;它只是让您知道您没有使用网络安全配置。如果您想添加一个,请查看 Android 开发者网站上的此页面:https://developer.android.com/training/articles/security-config.html

【讨论】:

  • 每当我尝试添加这一行(来自developer.android.com/training/articles/security-config.html):android:networkSecurityConfig="@xml/network_security_config" 它给了我红线 - 无法解析符号'@ xml/network_security_config'less... (Ctrl+F1) 验证 Android XML 文件中的资源引用。
【解决方案2】:

试试这些解决方案

解决方案 1)

将以下属性添加到AndroidManifest.xml 中的&lt;application 标签:

android:usesCleartextTraffic="true"

解决方案 2)

android:networkSecurityConfig="@xml/network_security_config" 添加到app/src/main/AndroidManifest.xml 中的&lt;application 标签:

<application
        android:name=".ApplicationClass"
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

app/src/main/res/xml/ 中有对应的network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

【讨论】:

    【解决方案3】:

    你不需要做任何事情。如果您想将应用程序限制为仅访问某些服务器,或者如果您想为通常不在信任链中的服务器提供 SSL 证书,则添加网络配置很有用。如果这些都不适用于您,则没有理由不使用默认值。

    【讨论】:

    • 我正在寻找我正在输入的站点的源代码(在代码中,您可以看到“posh24.se”作为示例,但它不起作用.. 也尝试过其他站点或使用 https 以及失败
    • @Roy,这既不是网络安全配置问题,也不是 http/https 问题。您收到的错误是调试消息(以 /D 开头)而不是错误消息。这是与线程相关的问题。请检查以下答案。
    猜你喜欢
    • 1970-01-01
    • 2017-07-15
    • 2017-02-17
    • 2017-09-15
    • 2019-09-23
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多