1

I'm currently learning how to build apps at android studio with udemy course of Rob Percival.

Just reached the point where I want to get info from the internet, I followed the instructions and didn't get the Log I expected (the info from the site).

got an error :

No Network Security Config specified, using platform default

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();
    }
}

also I added this line to the Manifest.xml: uses-permission android:name="android.permission.INTERNET"

What should I do?

Roy
  • 11
  • 1
  • 1
  • 3

3 Answers3

1

The message you're getting isn't an error; it's just letting you know that you're not using a Network Security Configuration. If you want to add one, take a look at this page on the Android Developers website: https://developer.android.com/training/articles/security-config.html.

  • Whenever i try to add this line (from https://developer.android.com/training/articles/security-config.html): *android:networkSecurityConfig="@xml/network_security_config"* it gives me red line - Cannot resolve symbol '@xml/network_security_config' less... (Ctrl+F1) Validates resource references inside Android XML files. – Roy Oct 02 '17 at 17:43
1

Try these solutions

Solution 1)

Add the following attribute to the <application tag in AndroidManifest.xml:

android:usesCleartextTraffic="true"

Solution 2)

Add android:networkSecurityConfig="@xml/network_security_config" to the <application tag in 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">

With a corresponding network_security_config.xml in app/src/main/res/xml/:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

YOu don't need to do anything. Adding a network config is useful if you want to restrict your app to only hitting certain servers, or if you want to provide an SSL cert for a server who wouldn't normally be in the trust chain. If neither of those apply to you, there's no reason not to use the default.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Im looking to get the source code of the site im entering (in the code you can see "http://www.posh24.se/" as an example but it doesnt work.. tryed also other sites or using https and also failed – Roy Oct 02 '17 at 19:20
  • @Roy, It is neither a network security configuration issue nor a http/https issue. The error you are getting is debug message (starts with /D) and not an error message. It is thread related issue. Please check below answer. – Om Prakash Nov 12 '18 at 12:01