0

The following piece of code runs successfully if the android version is 6 or 7 but crashes for android version 9

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

String IPConnection ="http://************";

public String doInBackground(String... IntegerParam) {
    try {               
        URL url = new URL(IPConnection+"TestConnectionToServer");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));    
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }

            bufferedReader.close();         
            return stringBuilder.toString();                
        }                   
        finally {
            urlConnection.disconnect();
        }
    }       
    catch(Exception e) {        
        Log.e("ERROR", e.getMessage(), e);
        return e.getMessage();     
    }   
}
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
Hany Gaber
  • 11
  • 2

1 Answers1

0

One network related change with Android 9 is that it's no longer easy to use http instead of https, see Protecting users with TLS by default in Android P.

Most likely you can just switch to https.

If you want to/ have to stay with http, you have to change your app's network security config.

  • Create a resource directory named xml in the main/res folder
  • Create an XML resource file mynetworksecurityconfig.xml in this directory
  • To allow insecure cleartext connections to a specific domain, you can declare the network security configuration as follows:

mynetworksecurityconfig.xml

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">insecure.example.com</domain>
    </domain-config>
</network-security-config>
  • Finally, add a link to the config file in the app's Manifest.xml by setting the android:networkSecurityConfig attribute in the application tag

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.mynetworksecurityconfigtest">

    <application android:networkSecurityConfig="@xml/mynetworksecurityconfig"
                ... >
        ...
    </application>
</manifest>

See the training unit on Network security configuration for more information.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • when replace HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); TO HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); Get ERROR com.android.okhttp.internal.huc.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection – Hany Gaber Feb 17 '19 at 19:03
  • Did you change the prefix in `String IPConnection = ...` from http to https? – Bö macht Blau Feb 17 '19 at 19:07
  • Yes ... but get another error Hostname **** not verified certificate – Hany Gaber Feb 17 '19 at 19:49
  • Then it looks like you have to use http. See the two links for examples how to set up your network security configuration – Bö macht Blau Feb 18 '19 at 05:14
  • is this [network security configuration] for device or in my app code java ????????? Thanks advance – Hany Gaber Feb 18 '19 at 08:21
  • @Hany Gaber - The configuration is part of your app's code and will be valid only for your app. Please see my edited answer, I tried to sketch what you have to do – Bö macht Blau Feb 18 '19 at 18:37
  • many thanks for your effort ... this configuration require which minimum API version ?? note .. I user eclipse tools – Hany Gaber Feb 19 '19 at 07:20
  • @Hany Gaber - they say in the linked blog post that they added the network security config feature in Nougat. So that's what I'd try. About using Eclipse on the other hand... well, it is not the recommended IDE for Android so I suppose it's "use at your own peril" ;-) – Bö macht Blau Feb 19 '19 at 18:04
  • error: No resource identifier found for attribute 'networkSecurityConfig' in package 'android' – Hany Gaber Feb 22 '19 at 20:43
  • error: No resource identifier found for attribute 'networkSecurityConfig' in package 'android' is there any solution ?????? note I update SDK to API 28 – Hany Gaber Feb 22 '19 at 20:50
  • @Hany Gaber - sorry but I can't reproduce your situation. I just tested again with an app with minSDK 19 and compileSDK = target SDK = 28. Once I used explicitly the build tools version 27.0.3, once I let Android Studio use the default build tools version (gradle version 3.1.3). Support lib version was always "28.0.0". The only thing I had to add because of the low minSDK was `tools:targetApi="24"`in the application tag of the Manifest. See also [this post](https://stackoverflow.com/q/52691058/5015207) on supporting lower android versions. My sample compiles and runs on Kitkat and on Oreo – Bö macht Blau Feb 23 '19 at 16:23
  • @Hany Gaber - if you keep running into problems I think it's best if you ask a new question with an [MCVE] of your current approach. Comments are not really a good format for remote debugging – Bö macht Blau Feb 23 '19 at 16:26