Q: Android 9.0: HTTP request throws exception in "Minimize mode" Every HTTP request throws failed to connect to Server wherever App goes in sleep or minimize mode. This only happens in Android Pie(9.0).
Please check my code below,
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AsyncReadConfig asyncReadConfig = new AsyncReadConfig();
                asyncReadConfig.execute();
            }
        });
    }

    class AsyncReadConfig extends AsyncTask<Integer, Integer, Object> {
        @Override
        protected Object doInBackground(Integer... integers) {
            try {
                return callGetURLService(CONFIGURATION_URL);
            } catch (Exception e) {
                return e;
            }
        }
    }

    private Object callGetURLService(String path) {
        try {
            URL url = new URL(path);

            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String text;
            StringBuilder configParams = new StringBuilder();
            while ((text = in.readLine()) != null) {
                configParams.append(text);
            }

            JSONObject jsonObject = new JSONObject(configParams.toString());
            //Doing my stuff

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

In my manifeast.xml,
<uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        .
        .

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

I'm just reading text file from server in sleep mode and I'm getting error,

java.net.ConnectException: Failed to connect to server

And as I'm switching my App from minimize mode to active mode, everything is working fine.

A: When your app is in the background you cant make network calls unless you are using a Foreground service to make the network call
