Android异步POST请求PHP接口写入MySQL时触发java.io.FileNotFoundException的问题排查
Hey there, let's break down why you're hitting this FileNotFoundException when making your POST request—especially since it worked before but suddenly stopped. Here are the most likely culprits and how to fix them:
1. You're Ignoring Non-200 HTTP Responses
This is the #1 cause of this specific exception with HttpURLConnection. When the server returns an error code (like 404, 403, 500), the library throws FileNotFoundException instead of letting you read the error response.
Your code calls getInputStream() directly without checking the response status. Instead, you need to first check the response code, then use getErrorStream() for non-successful responses:
Modified Code Snippet
String users_url = "https://gdihq.com/evaluate/register.php"; String method = params[0]; if(method.equals("Users")){ String Date = params[1]; String FirstName = params[2]; String LastName = params[3]; String Phone = params[4]; String Email = params[5]; try { URL url = new URL(users_url); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); // Add Content-Type header (critical for PHP to parse POST data) httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); OutputStream OS = httpURLConnection.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8")); String data = URLEncoder.encode("mDate","UTF-8") + "=" + URLEncoder.encode(Date,"UTF-8") + "&" + URLEncoder.encode("FirstName","UTF-8") + "=" + URLEncoder.encode(FirstName,"UTF-8") + "&" + URLEncoder.encode("LastName","UTF-8") + "=" + URLEncoder.encode(LastName,"UTF-8") + "&" + URLEncoder.encode("Phone","UTF-8") + "=" + URLEncoder.encode(Phone,"UTF-8") + "&" + URLEncoder.encode("Email","UTF-8") + "=" + URLEncoder.encode(Email,"UTF-8"); bufferedWriter.write(data); bufferedWriter.flush(); bufferedWriter.close(); OS.close(); // Check response code first int responseCode = httpURLConnection.getResponseCode(); InputStream inputStream; if (responseCode >= 200 && responseCode < 300) { inputStream = httpURLConnection.getInputStream(); } else { inputStream = httpURLConnection.getErrorStream(); } BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8")); // Use UTF-8 for better compatibility StringBuilder result = new StringBuilder(); // More efficient than string concatenation String line = ""; while ((line = bufferedReader.readLine()) != null){ result.append(line); } bufferedReader.close(); inputStream.close(); httpURLConnection.disconnect(); Log.d(TAG, "Response Code: " + responseCode + ", Server Response: " + result.toString()); return result.toString(); } catch (MalformedURLException e) { Log.d(TAG,"Invalid URL Error", e); } catch (IOException e) { Log.d(TAG,"Network/IO Error", e); } } return null;
2. Server-Side Issues (PHP/MySQL or Configuration)
Since it worked before, something likely changed on the server:
- The
register.phpfile was moved, deleted, or its file permissions were modified (ensure it has755permissions). - Your MySQL credentials changed, or the database user lost write access.
- The server's
.htaccessrules were updated, blocking or redirecting your POST request.
Test This First
Use curl or Postman to send a manual POST request to your endpoint:
curl -X POST -d "mDate=2021-01-09&FirstName=Test&LastName=User&Phone=1234567890&Email=test@example.com" https://gdihq.com/evaluate/register.php
Check the response code and body—this will tell you exactly what the server is returning (e.g., a 500 error with PHP stack trace, or a 404 if the file is missing).
3. HTTPS Certificate/SSL Issues
Android's HttpsURLConnection strictly validates SSL certificates. If your server's certificate expired, was reissued, or isn't trusted by the device's root store, it can cause this exception (even if it worked before).
Quick Check
Open the URL https://gdihq.com/evaluate/register.php in a mobile browser on the same device. If you see a certificate warning, that's the issue. For production, you'll need a valid, trusted SSL certificate. For testing, you can temporarily bypass certificate validation (never do this in production!).
4. Network/Proxy Interference
If the device is on a new network (like a corporate VPN or public Wi-Fi), a proxy or firewall might be blocking the request. Test the request on a different network (e.g., your home Wi-Fi) to rule this out.
内容的提问来源于stack exchange,提问作者Jeswills

