即使连接处于活动状态,HTTP 请求也会失败

use*_*558 12 java networking android

我在HTTP.java.

在桌面上一切正常(不执行该请求,因为只有在 Android 上才需要)。

在Android中,没有那个http请求就可以正常工作。

在发出该 http 请求之后,所有其他请求都会在超时后失败并出现 UnknownHostException 错误,就好像他们不再有权访问 Internet 连接,即使它处于活动状态。同样在几分钟后和 onResume 之后,所有 http 请求都失败了。尽管该应用程序无法获得 http 响应,但 AdMob 广告出现在我的 App 中,所以我认为连接可能有效(或者 AdMob 库是否缓存了一些广告并在需要时显示它们?)。

通常,在这些情况下,它有时会再次起作用:

  • 从 Android Studio 卸载并重新安装应用程序
  • 关闭应用程序,等待几分钟并重新打开它
  • 从设备清除应用程序数据
  • 等待不确定的时间

? 多次尝试后发现问题一出现,几乎每次,如果我激活飞行模式然后禁用它,数据连接被停用然后重新激活,并且App可以立即执行所有后续http请求,而不必在您的设备上执行任何其他操作,甚至无需重新打开应用程序。

  • 在 1GB fibra 网络上用 wifi 测试:同样的错误
  • 我检查了连接:它是稳定的,在 wifi 和 SIM 卡中
  • 在清单中有使用互联网的许可(否则它永远不会工作)
  • 同时,桌面上的同一个App完美运行,速度最快,不到1秒收到http响应,所以服务器不是问题
  • 我还使用 url https://www.google.it进行了测试:同样的错误,在不到 1 秒的时间内通过浏览器可以在设备中访问相同的 url
  • 就像在.timeout(10000)我使用 10 秒的超时行中一样,服务器已将超时设置为 60 秒
  • https://www.ssllabs.com/ssltest检查服务器 SSL“质量” :在“总体评级”中获得“A”
  • 直接与 Vodafone 运营商完成 SIM 卡“重启”

我正在测试具有平面 4G 连接和 wifi 的真实设备,并安装了“NetGuard - 无根防火墙” VPN 应用程序

此 VPN 与所有其他应用程序都可以很好地运行,可能我的应用程序也是如此,但我不能排除这是问题所在,尽管我几乎不认为它是(我多年来一直没有问题地使用它)。

我的代码有问题吗?或者至少,我怎么知道这个错误的确切原因?

谢谢。

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);
}
Run Code Online (Sandbox Code Playgroud)

我的游戏.java:

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

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

    this.setScreen(new MainMenuScreen(this));
}
Run Code Online (Sandbox Code Playgroud)

MainMenuScreen.java:

public MainMenuScreen(final MyGame game) {
    ... 
    
    performHttpRequest(baseUrl, getData, true, myListener);
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

build.gradle(模块android)

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

build.gradle(项目)

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"
    }
}
Run Code Online (Sandbox Code Playgroud)

日志猫:

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)
Run Code Online (Sandbox Code Playgroud)

(我去掉libGDX标签是因为它看起来与Android相关,因为该方法只是通过接口从MainMenuScreen类调用,而是在Android Activity中执行)

adb logcat *:E正如Always Learning在评论中所建议的那样,日志是从应用程序打开到 http 请求结束:

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
Run Code Online (Sandbox Code Playgroud)

编辑 1 ------------------------------------------------ ---------

我刚刚与 Vodafone 运营商直接执行了 SIM 卡“重启”,所以现在我应该排除 SIM 卡问题。

http 请求在 MainMenuScreen 中失败时的一些信息,但显示了 AdMod 广告,并且在 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
]
Run Code Online (Sandbox Code Playgroud)

错误:

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(StreamA

小智 2

该错误可能与 VPN 连接有关,请关闭应用程序,关闭 VNP(如果已连接),重新连接到 VPN 并重新启动应用程序。

在任何情况下,您都必须在打开应用程序之前连接 VPN。