13

I'm facing a problem with an http request done in HTTP.java.

On desktop all works fine (that request is not performed because it's necessary only on Android).

In Android all works without that http request.

After that that http request is made, all others fail after timeout with UnknownHostException error, as if they no longer have access to the internet connection, even if it is active. Also after minutes and after the onResume all http requests fail. Although the app cannot get an http response, the AdMob ad appears in my App, so I think that probably the connection works (or does the AdMob library cache some ads and show them when needed?).

Often, in these cases sometimes it works again:

  • uninstalling and reinstalling the App from Android Studio
  • closing the App, waiting some minutes and reopening it
  • clearing App data from the device
  • waiting an undefined amount of time

After several attempts I discovered that as soon as the problem occurs, almost every time, if I activate the airplane mode and then disable it, the data connection is deactivated and then reactivated, and the App can immediately execute all subsequent http requests without having to do anything else on your device and without even having to reopen the application.

  • tested with wifi on a 1GB fibra network: same error
  • I checked the connection: it is stable, in wifi and also with SIM
  • in the manifest there is the permission for using internet (otherwise it would never have worked)
  • at the same time, the same App on desktop works perfectly and at the best speed, receiving http responses in less than 1 second, so the server isn't the problem
  • I tested also with the url https://www.google.it: same error, the same url is reachable in the device via browser in less than 1 second
  • as in the line .timeout(10000) I'm using a timeout of 10 seconds, the server has timeout set to 60 seconds
  • checked the server SSL "quality" at https://www.ssllabs.com/ssltest: got "A" in "Overall Rating"
  • done the SIM "reboot" directly with a Vodafone operator

I'm testing on a real device with a flat 4G connection and with wifi, with "NetGuard - no-root firewall" VPN App installed.

This VPN works very well with all the other apps and probably mine too, but I can't rule out that it's the problem, although I hardly think it is (I use it since years without problems).

There is something wrong in my code? Or at least, how can I know the exact cause of this error?

Thanks.

AndroidLauncher.java:

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    // Create the layout
    RelativeLayout layout = new RelativeLayout(this);

    // Do the stuff that initialize() would do for you
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    // Add the libGDX view
    game = new MyGame(this);
    gameView = initializeForView(game);
    layout.addView(gameView);

    // Add the AdMob view
    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    layout.addView(mAdView, adParams);

    layout.setKeepScreenOn(true);

    setContentView(layout);
}

MyGame.java:

public class MyGame extends Game {
    public void create() {
    
    ...

    java.security.Security.setProperty("networkaddress.cache.ttl" , "0");

    this.setScreen(new MainMenuScreen(this));
}

MainMenuScreen.java:

public MainMenuScreen(final MyGame game) {
    ... 
    
    performHttpRequest(baseUrl, getData, true, myListener);
}

HTTP.java:

public class HTTP {
    public static void performHttpRequest(String baseUrl, String q, Net.HttpResponseListener httpResponseListener) {
        HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
        Net.HttpRequest httpRequest = requestBuilder.newRequest()
            .method(Net.HttpMethods.GET)
            .url(baseUrl)
            .content(q)
            .timeout(10000)
            .build();
        Gdx.net.sendHttpRequest(httpRequest, httpResponseListener);
    }
}

build.gradle (module android):

...
buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            debuggable true
        }
    }
...

build.gradle (project):

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.1'
    }
}

allprojects {
    apply plugin: "eclipse"

    version = '1.0'
    ext {
        appName = "MyAppName"
        gdxVersion = '1.9.13'
        roboVMVersion = '2.3.8'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.8.0'
    }

    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        google()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }
}

project(":desktop") {
    apply plugin: "java-library"

    dependencies {
        implementation project(":core")
        api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        api "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
        api "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
        api "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-desktop"
        api "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
    }
}

project(":android") {
    apply plugin: "android"

    configurations { natives }

    dependencies {
        implementation project(":core")
        api "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
        api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
        api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
        api "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-x86_64"
        implementation 'com.google.android.gms:play-services-ads:19.6.0'
        implementation 'com.google.android.gms:play-services-basement:17.5.0'
    }
}

project(":core") {
    apply plugin: "java-library"

    dependencies {
        api "com.badlogicgames.gdx:gdx:$gdxVersion"
        api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        api "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
    }
}

Logcat:

I/System.out: failed to connect to myDomain.app/NN.NN.NN.NN (port 443) from /10.NN.NN.NN (port 41326) after 10000ms
W/System.err: java.net.SocketTimeoutException: failed to connect to myDomain.app/NN.NN.NN.NN (port 443) from /10.NN.NN.NN (port 41326) after 10000ms
        at libcore.io.IoBridge.connectErrno(IoBridge.java:191)
        at libcore.io.IoBridge.connect(IoBridge.java:135)
        at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
        at java.net.Socket.connect(Socket.java:621)
        at com.android.okhttp.internal.Platform.connectSocket(Platform.java:182)
        at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:1407)
        at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:1359)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:221)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:144)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:106)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:400)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:333)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:483)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:135)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
        at com.badlogic.gdx.net.NetJavaImpl$2.run(NetJavaImpl.java:223)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)

(I removed the libGDX tag because it seems Android related, because the method is only invoked from the MainMenuScreen class through the interface, but executed in the Android activity)

adb logcat *:E as suggested by Always Learning in a comment, the log is from the App opening to the end of the http request:

cognitionService: handleMessage: event 200 value : 1
02-23 19:39:55.336  5803  5803 E libprocessgroup: set_timerslack_ns write failed: Operation not permitted
02-23 19:39:55.347  5803  5803 E libprocessgroup: set_timerslack_ns write failed: Operation not permitted
02-23 19:39:57.163  4987  5272 E ActivityTaskManager: TouchDown intent received, starting ActiveLaunch
02-23 19:39:57.205  4987  5041 E system_server: Invalid ID 0x00000000.
02-23 19:39:57.212 12157 12157 E .myAppName: Unknown bits set in runtime_flags: 0x8000
02-23 19:39:57.233  4987  5041 E DecorView: mWindow.mActivityCurrentConfig is null
02-23 19:39:57.281  4500  4613 E BufferQueueProducer: [com.sec.android.app.launcher/com.sec.android.app.launcher.activities.LauncherActivity$_5803#0] disconnect: not connected (req=1)
02-23 19:39:57.281  5803  7530 E OpenGLRenderer: ReliableSurface: perform returned an error
02-23 19:39:57.295  4987  5234 E PkgPredictorService-Collector: record changed bt=0  wifi=0 screen=0
02-23 19:39:57.932 14641 14641 E webview_servic: Not starting debugger since process cannot load the jdwp agent.
02-23 19:39:57.935  4987  5540 E PackageManager: Failed to find package; permName: android.permission.INTERNET, uid: 99774, caller: 1000
02-23 19:39:57.962 14661 14661 E ocessService0:: Not starting debugger since process cannot load the jdwp agent.
02-23 19:39:58.627  4987  5307 E WindowManager: win=Window{5a5aa90 u0 com.sec.android.app.launcher/com.sec.android.app.launcher.activities.LauncherActivity} destroySurfaces: appStopped=true win.mWindowRemovalAllowed=false win.mRemoveOnExit=false win.mViewVisibility=8 caller=com.android.server.wm.AppWindowToken.destroySurfaces:1248 com.android.server.wm.AppWindowToken.destroySurfaces:1229 com.android.server.wm.AppWindowToken.notifyAppStopped:1284 com.android.server.wm.ActivityRecord.activityStoppedLocked:2776 com.android.server.wm.ActivityTaskManagerService.activityStopped:2512 android.app.IActivityTaskManager$Stub.onTransact:2288 android.os.Binder.execTransactInternal:1056 
02-23 19:39:58.708  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:39:58.728  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:39:58.775  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:39:58.923  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:39:58.924  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:39:58.981  4987  5041 E WindowManager: win=Window{b1dc417 u0 Splash Screen my.package.name EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0 caller=com.android.server.wm.AppWindowToken.destroySurfaces:1248 com.android.server.wm.AppWindowToken.destroySurfaces:1229 com.android.server.wm.WindowState.onExitAnimationDone:5189 com.android.server.wm.WindowStateAnimator.onAnimationFinished:320 com.android.server.wm.WindowState.onAnimationFinished:5630 com.android.server.wm.-$$Lambda$yVRF8YoeNdTa8GR1wDStVsHu8xM.run:2 com.android.server.wm.SurfaceAnimator.lambda$getFinishedCallback$0$SurfaceAnimator:100 
02-23 19:39:59.120 12157 12157 E libc    : Access denied finding property "ro.serialno"
02-23 19:39:59.354  4447  4447 E audit   : type=1400 audit(1614105599.351:37597): avc:  denied  { write } for  pid=12157 comm="Thread-12" name="perfd" dev="sda32" ino=35421 scontext=u:r:untrusted_app:s0:c112,c257,c512,c768 tcontext=u:object_r:shell_data_file:s0 tclass=dir permissive=0 SEPF_SM-M315F_10_0024 audit_filtered
02-23 19:39:59.354  4447  4447 E audit   : type=1300 audit(1614105599.351:37597): arch=c00000b7 syscall=48 success=no exit=-13 a0=ffffff9c a1=7b243f7ec1 a2=2 a3=0 items=0 ppid=4460 pid=12157 auid=... uid=10368 gid=10368 euid=10368 suid=10368 fsuid=10368 egid=10368 sgid=10368 fsgid=10368 tty=(none) ses=... comm="Thread-12" exe="/system/bin/app_process64" subj=u:r:untrusted_app:s0:c112,c257,c512,c768 key=(null)
02-23 19:39:59.354  4447  4447 E audit   : type=1327 audit(1614105599.351:37597): proctitle="my.package.name"
02-23 19:39:59.860  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:00.275  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:00.910 29776 14898 E memtrack: Couldn't load memtrack module
02-23 19:40:01.889  4500  4613 E BufferQueueProducer: [Toast$_12157#0] disconnect: not connected (req=1)
02-23 19:40:01.889 12157 14616 E OpenGLRenderer: ReliableSurface: perform returned an error
02-23 19:40:05.264  4987  5166 E MotionRecognitionService: handleMessage: event 200 value : 1
02-23 19:40:05.861  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:06.165  4987  5049 E NotificationService: Suppressing notification from package by user request.
02-23 19:40:06.272  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:09.116  4987  5043 E Watchdog: !@Sync: 19566 heap: 96 / 119 [2021-02-23 19:40:09.116] sdogWay: softdog
02-23 19:40:15.247  4987  5166 E MotionRecognitionService: handleMessage: event 200 value : 1
02-23 19:40:16.486  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:16.490  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:16.512  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:16.533  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:17.865  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:18.274  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:21.181  4987  5049 E NotificationService: Suppressing notification from package by user request.
02-23 19:40:25.335  4987  5166 E MotionRecognitionService: handleMessage: event 200 value : 1
02-23 19:40:30.587  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:30.616 12157 12157 E Ads     : JS: Uncaught ReferenceError: OmidCreativeSession is not defined (https://googleads.g.doubleclick.net/mads/gma?caps=inlineVideo_interactiveVideo_mraid1_mraid2_mraid3_sdkVideo_exo3_th_autoplay_mediation_scroll_av_av_transparentBackground_sdkAdmobApiForAds_di_aso_sfv_dinm_dim_nav_navc_dinmo_ipdof_gls_gcache_saiMacro_xSeconds&eid=...)
02-23 19:40:30.658 12157 12157 E Ads     : JS: Uncaught ReferenceError: OmidCreativeSession is not defined (https://googleads.g.doubleclick.net/mads/gma?caps=mraid1_mraid2_mraid3_th_mediation_scroll_av_av_transparentBackground_sdkAdmobApiForAds_di_aso_dinm_dim_dinmo_ipdof_gls_saiMacro_xSeconds&eid=...)
02-23 19:40:31.277  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:31.437  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:35.380  4987  5166 E MotionRecognitionService: handleMessage: event 200 value : 1
02-23 19:40:36.199  4987  5049 E NotificationService: Suppressing notification from package by user request.
02-23 19:40:36.586  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:37.279  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:37.443  4459  4647 E Netd    : getNetworkForDns: getNetId from enterpriseCtrl is netid 0
02-23 19:40:39.129  4987  5043 E Watchdog: !@Sync: 19567 heap: 87 / 110 [2021-02-23 19:40:39.129] sdogWay: softdog

Edit 1 ---------------------------------------------------------:

I just performed the SIM "reboot" directly with a Vodafone operator, so now I should exclude a SIM problem.

Some info while the http request fails in the MainMenuScreen but the AdMod ad is displayed and also all works fine in firefox:

activeNetwork.toString()           = 601
activeNetwork.describeContents()   = 0
activeNetworkInfo.getReason()      = null
activeNetworkInfo.getType()        = 0
activeNetworkInfo.getTypeName()    = MOBILE
activeNetworkInfo.getSubtype()     = 13
activeNetworkInfo.getSubtypeName() = LTE
activeNetworkInfo.getExtraInfo()   = mobile.vodafone.it
activeNetworkInfo.toString()       = [
    type:        MOBILE[LTE]
    , state:     CONNECTED/CONNECTED
    , reason:    (unspecified)
    , extra:     mobile.vodafone.it
    , failover:  false
    , available: true
    , roaming:   false
]

The errors:

W/System.err: java.net.SocketTimeoutException: SSL handshake timed out
W/System.err:     at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
        at com.android.org.conscrypt.NativeSsl.doHandshake(NativeSsl.java:387)
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:234)
        at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:1471)
        at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:1415)
        at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:1359)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:221)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:144)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:106)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:400)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:333)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:483)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:135)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
        at com.badlogic.gdx.net.NetJavaImpl$2.run(NetJavaImpl.java:223)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)

W/System.err: javax.net.ssl.SSLHandshakeException: SSL handshake aborted: ssl=0x7021beca08: I/O error during system call, Connection reset by peer
        at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
W/System.err:     at com.android.org.conscrypt.NativeSsl.doHandshake(NativeSsl.java:387)
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:234)
        at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:1471)
        at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:1415)
        at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:1359)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:221)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:144)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:106)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:400)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:333)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:483)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:135)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
        at com.badlogic.gdx.net.NetJavaImpl$2.run(NetJavaImpl.java:223)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)

My solution:

Uninstall the VPN App.

It was such a simple solution ... I would not have imagined it was the App's fault for the VPN, I considered it foolproof having used it for over 3 years ...

If this hadn't been the problem, your suggestions would certainly have been helpful and probably decisive, thank you all! (unfortunately I cannot purposely give the bounty reward to multiple users, but I can upvote your answer)

user2342558
  • 5,567
  • 5
  • 33
  • 54
  • Did you declare `android.permission.INTERNET` in your manifest? `` – Always Learning Feb 23 '21 at 19:05
  • @AlwaysLearning sure. As I said, the problem doesn't always occurs, without any change to the code. Sometimes it all works fine, other times that error occurs – user2342558 Feb 23 '21 at 19:06
  • I would just double check that you have that permission based off of the logs. If not, I suggest adding it and seeing if that fixes the issue. – Always Learning Feb 23 '21 at 19:07
  • I just saw that in the manifest the permission is set and also granted as indicated in the application settings on the device – user2342558 Feb 23 '21 at 19:08
  • Ok cool, thank for confirming. What is `enterpriseCtrl`? That doesn't appear to be an Android construct? Also, why is netd calling it? The fact that it is returning 0 means you aren't getting a network to use which could explain your issue. – Always Learning Feb 23 '21 at 19:17
  • @AlwaysLearning Unfortunately I don't know that. Maybe it's called by a native libGDX class like gdx.net. – user2342558 Feb 23 '21 at 19:20
  • @AlwaysLearning now the error has changed to: `java.net.UnknownHostException: Unable to resolve host "mydomain.app": No address associated with hostname` and `java.net.UnknownHostException: Unable to resolve host "www.mydomain.app": No address associated with hostname` – user2342558 Feb 23 '21 at 19:20
  • also, on the same device I have another my App that use the same server and it works perfectly instantly – user2342558 Feb 23 '21 at 19:28
  • ```System.setProperty("http.proxyHost", "127.0.0.1"); System.setProperty("http.proxyPort", "80"); System.setProperty("https.proxyHost", "127.0.0.1"); System.setProperty("https.proxyPort", "443");``` – Darkman Feb 24 '21 at 09:34
  • Can you remove the `proguardFiles...` line from your gradle file and test again after rebuilding? – Always Learning Feb 24 '21 at 16:55
  • @AlwaysLearning its the same – user2342558 Feb 24 '21 at 20:45
  • Not a solution but some remarks: From your log it seems your timeout settings are ignored. You are setting the values to 25000 but the message says it times out after 10000. I would try: 1) remove timeouts completely 2) log a stack-trace where you catch the exception 3) create a new dummy application with just a button which when clicked makes a request in order to troubleshoot further. Hope it helps – c.s. Feb 27 '21 at 08:16
  • don't set connection timeout – Usama Altaf Mar 01 '21 at 10:10
  • @UsamaAltaf why? The server is faster responding below 1 second. – user2342558 Mar 01 '21 at 11:55
  • @user2342558 - can you please try the following: 1. install the app, try it on your sim internet (4g). after that close the app. open wifi. try it again. Please let me know if the error happen at this scenario. 2. The second thing I want you to try is to clear the app cache from the OS and try it. give me the result to that as well. 3. The last thing is: What is the domain you are trying to reach? and also - which OS are you using? from which type of phone? I had some issues with that as well that happened because the OS TCP handling was different for the OS comes with xioami. Thanks! – ALUFTW Mar 01 '21 at 19:14
  • One more important thing- try to put the url with everything happens after the /... and let us know what happens. https://stackoverflow.com/questions/44617313/retrofit-2-error-java-net-sockettimeoutexception-failed-to-connect-to-192-168 – ALUFTW Mar 01 '21 at 19:21
  • did you add "security configue" in your app . check this https://stackoverflow.com/a/53984915/9474700 – behrad Mar 02 '21 at 08:09
  • @behrad I don't need to set `usesCleartextTraffic=true` because my server already has SSL. Anyway, that cannot be a solution. (also, that answer is 1 or 2 years old, when the SSL weren't so widely used. Now almost all connections use SSL, also because all browsers warns if the URL is without it) – user2342558 Mar 02 '21 at 08:20
  • it looks like it could be that the Wi-Fi signals are weak on that device, maybe this can help you: https://stackoverflow.com/questions/37314712/how-to-resolve-java-net-unknownhostexception – borchvm Mar 02 '21 at 13:15
  • @borchvm I'm not using wifi on the device – user2342558 Mar 02 '21 at 13:23
  • as per java doc, setConnectTimeout(..) says non-standard implementation is ignoring timeout specified in argument. Can you see what's timeout value you got when invoking getConnectTimeout() – Ashish Patil Mar 02 '21 at 14:39
  • @AlwaysLearning thanks, I made some question edits after discovering new info, please see it – user2342558 Mar 04 '21 at 20:53
  • @c.s. thanks, I made some question edits after discovering new info, please see it – user2342558 Mar 04 '21 at 20:54
  • @borchvm thanks, I made some question edits after discovering new info, please see it – user2342558 Mar 04 '21 at 20:54
  • @AshishPatil thanks, I made some question edits after discovering new info, please see it – user2342558 Mar 04 '21 at 20:54
  • I think I know the answer. Can you log the "active network" from somewhere in the app, maybe right before you make your http request? Here is the api to use `ConnectivityManager.getActiveNetwork() `: https://developer.android.com/reference/android/net/ConnectivityManager#getActiveNetwork() Let us know what that returns. It sounds like you app is getting a network initially that doesn't have connectivity. When you do airplane mode on/off, the network is then set to a different one that does have connectivity. Based off of that info, I can potentially provide a full answer. – Always Learning Mar 04 '21 at 21:48
  • @AlwaysLearning I added a method to get that info, right before the call to `performHttpRequest`. Now that the connection works it returns `601` (with the VPN active on the device). As soon as it doesn't work I'll post it. Do you need only this or other info? thanks – user2342558 Mar 05 '21 at 09:56
  • The network and the transport type would be helpful.That should do it. – Always Learning Mar 05 '21 at 15:24
  • @AlwaysLearning please see my "Edit 1" – user2342558 Mar 05 '21 at 18:33
  • @user2342558 You mentioned VPN. If you disable the VPN, does the problem persist? I suspect, it will work fine without the VPN and in that case this vpn is blocking your connection. – Sherif elKhatib Mar 11 '21 at 11:12
  • @SherifelKhatib I'll try your suggestion, thanks. If you're true, I'll probably change the App for VPN... – user2342558 Mar 11 '21 at 14:17
  • @SherifelKhatib insert it as an answer, so if that was the problem, I'll can set you for the bounty reward. – user2342558 Mar 11 '21 at 14:18
  • @SherifelKhatib after almost 1 day without the VPN, all works fine! It was the problem... thanks! I had to try without VPN right away ... post your answer – user2342558 Mar 12 '21 at 08:20
  • @user2342558 Glad I could help I have been away from SO for years – Sherif elKhatib Mar 13 '21 at 12:21

3 Answers3

2

That error maybe relate to VPN connect, please close the application, off VNP if it was connected, re-connect to VPN and restart you application.

In all case, you must be connect the VPN before open the application.

dinhlam
  • 708
  • 3
  • 14
  • thanks, I made some question edits after discovering new info, please see it – user2342558 Mar 04 '21 at 20:53
  • The problem was the VPN somehow interfering with the connection of my App (even without showing that the connection attempt was blocked ...). Not exactly what you suggested but your answer turned out to be the closest to the solution. – user2342558 Mar 12 '21 at 16:53
2

I don't know exactly how you make the request. But it seems you just call it from the main Thread which can be blocked by android. Maybe create a separate Thread for it. Or if it's a small request just add to the Activity:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
  • As mentioned here from a high-level libgdx user https://stackoverflow.com/a/50905005/2342558, libGDX' HTTPRequest is async out of the box. Otherwise I should get an error on Android on the first attempt to do it synchronously. – user2342558 Mar 09 '21 at 12:40
  • In the libgdx wiki, it doesn't talk about errors from android but more like unable to access the network like your issue. "On Android: You cannot access the network on the main thread without disabling strict mode. This is done to prevent network operations from hanging the main thread." Can you try what Christian said? – Nicolas Sagala Beaucage Mar 10 '21 at 00:54
1

The UnknownHostException means that it was unable to resolve the hostname. This is normal (from time to time) with slow or unstable network connections. However, Java is aggressive at caching DNS responses. The reason it does not work until you reinstall the app is that the bad DNS response was cached, so all subsequent calls fail. Changing the DNS cache ttl in your java app should fix it:

 networkaddress.cache.ttl = 0
 networkaddress.cache.negative.ttl = 0

In this case it's the negative.ttl setting you want -- subsequent lookups will be bypassed until that time has elapsed. But it's a good idea to set both properties to low values.

More details here: How to make Java honor the DNS Caching Timeout?

Nicholas Rees
  • 723
  • 2
  • 9
  • thanks, I made some question edits after discovering new info, please see it – user2342558 Mar 04 '21 at 20:53
  • 2
    Looks like you changed your question quite a bit. It used to say that you were getting an unknownhostexception. Now you’re getting an ssl timeout, which seems like a different problem. Side note: I see you added one of the properties to your code that I suggested, but not the other one (the negative property). – Nicholas Rees Mar 08 '21 at 14:40
  • I added only the first line of code because I don't know what's the purpose of the other one. I don't clearly understand why is needed`networkaddress.cache.negative.ttl`: It "Indicates the caching policy for un-successful name lookups from the name service" but why is it needed and how it is handled?? Logically, if the dns lookup fails, the next lookup must be performed online and not in cache... – user2342558 Mar 08 '21 at 15:01
  • Logically... you would think that's the way it should work. Unfortunately, that's not the way Java works. It caches failed lookups unless you tell it not to. – Nicholas Rees Mar 08 '21 at 17:57