Android studio-未指定Network Security Config

时间:2017-10-02 17:04:14

标签: java android

我目前正在学习如何在Android工作室使用Rob Percival的udemy课程构建应用程序。

刚刚到达我想从互联网上获取信息的地方,我按照说明操作,没有得到我预期的日志(来自网站的信息)。

收到错误:

  

使用平台默认值

未指定Network Security Config
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 =&#34; android.permission.INTERNET&#34;

我该怎么办?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

尝试这些解决方案

解决方案1)

将以下属性添加到<application中的AndroidManifest.xml标记中:

android:usesCleartextTraffic="true"

解决方案2)

android:networkSecurityConfig="@xml/network_security_config"添加到<application中的app/src/main/AndroidManifest.xml标记中:

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

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

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