You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 10+版本设备上SQL Server交互应用连接失败排查求助

Troubleshooting Android 10+ Connection Failures for Your SQL Server-linked App

Hey there, let's figure out why your app works on emulators and Huawei 7.x tablets but can't connect to the server on Samsung Android 10+ devices. Here are the most probable issues and fixes to test out:

1. Cleartext Traffic Restriction (Android 9+)

Android 9 (API 28) and above block unencrypted HTTP requests by default—this is likely the biggest culprit since your BASE_URL uses http:// instead of https://.

Fix Options:

  • Option 1: Allow cleartext traffic for your server (quick fix)
    Add this attribute to your <application> tag in AndroidManifest.xml:
    <application
        ...
        android:usesCleartextTraffic="true">
    
  • Option 2: Restrict cleartext to your specific server (more secure)
    Create a network_security_config.xml file in res/xml/:
    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">XXX.XXX.XXX.XXX</domain> <!-- Replace with your server IP -->
        </domain-config>
    </network-security-config>
    
    Then link it in your AndroidManifest.xml:
    <application
        ...
        android:networkSecurityConfig="@xml/network_security_config">
    
  • Option 3: Switch your server to HTTPS (most secure long-term solution)

2. Retrofit Instance Initialization Bug

Looking at your ApiClient code, you're creating a new Retrofit instance but never assigning it to the retrofit static variable. This means every call to getRetrofitInstance() creates a fresh instance, which isn't a direct connection failure cause but can lead to unexpected behavior.

Fix:

Update the method to assign the created instance:

public static Retrofit getRetrofitInstance() {
    if (retrofit == null){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

3. Permission Configuration Cleanup

Your manifest has redundant permission declarations—<uses-permission-sdk-23> is unnecessary since <uses-permission> works across all Android versions. Clean it up to avoid any potential conflicts:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Also, double-check that the Samsung device hasn't had its network permissions manually disabled (go to Settings > Apps > [Your App] > Permissions to confirm "Internet" is enabled).

4. Verify Server Reachability on the Samsung Device

Before diving deeper into code, confirm the server is actually accessible from the Samsung device:

  • Open the device's browser and navigate to http://XXX.XXX.XXX.XXX:XXXX/register.php—if it doesn't load, the issue is with network routing (e.g., firewall, VPN, or server not accessible from the device's network).
  • Try connecting the Samsung device to a mobile hotspot instead of WiFi to rule out local network restrictions.

5. Get Detailed Error Logs

Your current onFailure method only shows a generic message. Add detailed logging to pinpoint the exact error:

@Override
public void onFailure(@NonNull Call<Contact> call, @NonNull Throwable t) {
    t.printStackTrace(); // This prints the full error to Logcat
    alertConnexion("Echec de connection aux serveur: " + t.getMessage());
    dialog.dismiss();
}

Check Logcat for errors like UnknownHostException (server IP unreachable), SSLHandshakeException (HTTPS issues), or ConnectException (connection timeout)—this will give you concrete clues about what's wrong.

Start with the cleartext traffic fix first, as that's the most common issue for Android 10+ connection failures. Then work through the other steps if that doesn't resolve it.

内容的提问来源于stack exchange,提问作者Enintsoa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 20:42:27