AsyncHttpClient无法发送请求,Android6.0下POST请求失败求助
Hey there, let's walk through the most likely reasons your POST requests are failing on Android 6.0—especially since things worked fine before. Here's what to check step by step:
First, double-check your AndroidManifest.xml—even though Android 6.0 introduced runtime permissions, the INTERNET permission is a "normal" permission that only needs to be declared in the manifest, not requested at runtime. If you accidentally removed <uses-permission android:name="android.permission.INTERNET" />, your app will silently fail to send any network requests.
Also, if your code relies on checking network state before sending requests, make sure you have <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> declared too—missing this might break your app's logic for triggering requests.
While the android:usesCleartextTraffic flag was enforced starting in Android 7.0, if your app's targetSdkVersion is set to 24 or higher, Android 6.0 will still honor this restriction. If your server uses HTTP (not HTTPS), you'll need to:
- Add
android:usesCleartextTraffic="true"to the<application>tag in yourAndroidManifest.xml, or - Set up a network security config file that explicitly allows cleartext traffic for your server domain.
If you recently updated your app's target SDK version, this is a common gotcha.
Since your app worked before, it's easy to blame the device—but server changes are often the culprit:
- Did the server switch from HTTP to HTTPS? Android 6.0 has stricter SSL certificate validation than older versions. If the server uses a self-signed or untrusted certificate, your request will fail unless you explicitly trust it (note: this isn't recommended for production apps).
- Has the API endpoint, required headers, or request body format changed? Compare your app's request details (URL, headers, payload) against the server's current documentation or test the endpoint directly with a tool like Postman to confirm it's working.
- Could your device's IP be blocked by the server, or is rate limiting now enabled? Test the request from a different device on the same network to rule this out.
Android 6.0 introduced Doze Mode, which limits network activity when the device is idle (screen off, not charging). If your app sends requests in the background, Doze might be blocking them. To test:
- Temporarily turn off Battery Saver: Settings > Battery > Battery Saver > Turn Off Now
- Add your app to the battery optimization whitelist: Settings > Battery > Battery Optimization > Select your app > Choose "Don't optimize"
App Standby (for apps not used recently) can also restrict network access, so try opening your app actively before sending the request.
If you're using HttpPost (Apache HttpClient), keep in mind that Android 6.0 removed this library by default. To use it, you need to add this to your build.gradle file:
android { useLibrary 'org.apache.http.legacy' }
For other libraries like OkHttp or Retrofit, make sure you're handling exceptions properly—many failures happen silently because errors aren't logged. Add logging for exceptions like IOException or SSLHandshakeException to get specific details about what's going wrong. Also, double-check that you're setting the correct Content-Type header (e.g., application/json or application/x-www-form-urlencoded)—servers often reject requests with missing or incorrect content types.
If your device is connected to a proxy server or VPN, it might be blocking or modifying your POST request. Try disconnecting from any proxy/VPN and testing again. You should also check if your Wi-Fi network has firewall rules that block outgoing traffic to your server's port.
内容的提问来源于stack exchange,提问作者Kimozer Aljithr

