I have an application that works fine on below Android9 phones
. Recently I start testing my application on android 9
and facing a strange problem that simple https/http post-call is not working and library(com.loopj.android
) giving an exception that fails to connect. I know that there is a change related to network and I tried all the things which I find on the internet but still no luck. Here is my configuration:
Manifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
tools:targetApi="28"
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true">
</application>
network_security_config.xml in res/xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">my-backend.hostname.dk</domain>
<trust-anchors>
<certificates src="system" />
<certificates src="user" />
</trust-anchors>
</domain-config>
</network-security-config>
I am signing the app with proper Keystore: build.gradle:
signingConfigs {
release {
storeFile file("../myApp-release.keystore")
storePassword "abcd1234"
keyAlias "myApp-release"
keyPassword "abcd1234"
}
}
Also tried to use proper SSLFactory for httpclient:
AsyncHttpClient client = new AsyncHttpClient();
try {
KeyStore trustStore = MySSLSocketFactory.getKeystore();
MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
// MyCustomSSLFactory socketFactory = new MyCustomSSLFactory(trustStore);
client.setSSLSocketFactory(sf);
client.setTimeout(600000);
client.setConnectTimeout(10000);
} catch (Exception e) {
}
Also tried to use SSL mentioned in this post but nothing is working for me and still getting an exception whenever my program try to call backend REST API. exception:
07-12 13:49:08.435 5111-5129/com.myapp.cherinstall W/System.err: java.net.SocketTimeoutException: failed to connect to my-backend.hostname.dk/192.168.4.24 (port 8182) from /192.168.3.246 (port 48880) after 10000ms
I have rooted the phone and running this code in an App that has no activity but the only intent service. I did some tests and figure out that if I run my HTTP request through activity(Create new Activity within the same Application for testing purpose) only once, then sending intent to this InteneService also start works but without invoking any Activity when application installed and sending IntentService call is failing. I know that there are more restrictions Google added in Android 9 and I don't know which one I am missing now. Can someone please give me a clue.