Android中PHP仅返回单行数据,如何实现返回多行JSONArray?
Hey there! I see the issue you're facing—your PHP script is only returning a single row instead of an array of all matching locations, which is why your Android app can't parse the expected JSONArray. Let's fix this step by step.
The core problem right now is that $stmt->fetch() only grabs the first row of your query results. To get all matching entries, we need to loop through every row and collect them into an array. Here's the adjusted PHP code:
$problema = $_POST['problema']; $stmt = $conn->prepare("SELECT latitudine, longitudine, tip_problema FROM alerte WHERE tip_problema = ?"); $stmt->bind_param("s", $problema); $stmt->execute(); $stmt->store_result(); if($stmt->num_rows > 0){ $stmt->bind_result($latitudine, $longitudine, $tip_problema); // Initialize an empty array to hold all location entries $locatie = array(); // Loop through every row in the result set while($stmt->fetch()){ // Add each row's data to the array $locatie[] = array( 'latitudine'=>$latitudine, 'longitudine'=>$longitudine, 'tip_problema'=>$tip_problema ); } $response['error'] = false; $response['message'] = 'Alerta raportata'; $response['locatie'] = $locatie; // Now this is an array of objects, not a single object }else{ $response['error'] = true; $response['message'] = 'Eroare de identificare a alertelor'; } // Don't forget to output the final JSON response echo json_encode($response);
What changed?
- We initialized
$locatieas an empty array instead of a single associative array - The
while($stmt->fetch())loop runs until all rows are read, pushing each row's data into the$locatiearray - When encoded to JSON,
$response['locatie']will now be a JSONArray, which matches what your Android app expects
Your existing onPostExecute code is already set up to handle a JSONArray, so it should work seamlessly with the updated PHP response. A small optional improvement: add a null check for tip_problema to avoid potential exceptions:
@Override protected void onPostExecute(String s) { super.onPostExecute(s); try { JSONObject obj = new JSONObject(s); if (!obj.getBoolean("error")) { Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show(); JSONArray locatieArray = obj.getJSONArray("locatie"); for (int i = 0; i < locatieArray.length(); i++) { JSONObject locatie = locatieArray.getJSONObject(i); // Add markers only if coordinates exist if(!locatie.isNull("latitudine") && !locatie.isNull("longitudine")) { latitudine_sql = Double.valueOf(locatie.getString("latitudine")); longitudine_sql = Double.valueOf(locatie.getString("longitudine")); addMarker(latitudine_sql, longitudine_sql); } // Add null check for tip_problema if(!locatie.isNull("tip_problema")) { tip_problema_sql = locatie.getString("tip_problema"); } } } else { Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } }
With these changes, your PHP script will send all matching location rows to the Android app, which can then loop through them and add all the markers to your map.
内容的提问来源于stack exchange,提问作者Costin

