gpt4 book ai didi

java - 使用 php 和 android 从 sql 数据库中选择所有内容

转载 作者:行者123 更新时间:2023-11-29 20:59:18 26 4
gpt4 key购买 nike

https://www.dropbox.com/s/1uq36sbd6d7a3gr/Untitled.png?dl=0

我通过我的 android 应用程序连接到我的 .php 文件,然后该应用程序连接到我的 sql 数据库,然后它通过接收用户名并获取具有相同用户名的所有信息并将其显示在一个中来获取我保存在那里的所有信息。回收者查看器。到目前为止,它只向我展示了最后一只宠物。如何告诉我的代码显示该用户名下的所有宠物。

JAVA代码

**RECEIVE PET**
public class ReceivePet extends StringRequest {

private static final String RECEIVE_PET_URL = "http://lassie.netai.net/ReceivePet.php";
private Map<String, String> params;

public ReceivePet (String username, Response.Listener<String> listener) {
super(Request.Method.POST, RECEIVE_PET_URL, listener, null);
params = new HashMap<>();
params.put("username", username);
}

@Override
public Map<String, String> getParams() {
return params;
}

}

**WHERE I GET MY SQL DATABASE INFO**
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");

if (success) {
petName = jsonResponse.getString("petname");
petBreed = jsonResponse.getString("petbreed");
phoneNumber = jsonResponse.getString("phonenumber");
petDescription = jsonResponse.getString("petdescription");
imageUrl = jsonResponse.getString("imageurl");

petsList.add(new Pet(petName, petBreed, phoneNumber, petDescription, imageUrl));
adapter.notifyDataSetChanged();
} else {

}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
ReceivePet receiverPetRequest = new ReceivePet(username, responseListener);
RequestQueue queue = Volley.newRequestQueue(getActivity());
queue.add(receiverPetRequest);

PHP 代码

<?php
$con = mysqli_connect("mysql13.000webhost.com", "a9017958_omer", "pass123", "a9017958_users");

$username = $_POST["username"];

$statement = mysqli_prepare($con, "SELECT * FROM pets WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $petsID, $username, $petname, $petbreed, $phonenumber, $petdescription, $imageurl);

$response = array();
$response["success"] = false;

while(mysqli_stmt_fetch($statement)) {
$response["success"] = true;
$response["petname"] = $petname;
$response["petbreed"] = $petbreed;
$response["phonenumber"] = $phonenumber;
$response["petdescription"] = $petdescription;
$response["imageurl"] = $imageurl;
}

print_r (json_encode($response));

?>

最佳答案

PHPwhile 循环中,每次迭代时都会覆盖 $response 中的值。您需要创建一个数组的数组。这是一个例子:

$response = array();
while(mysqli_stmt_fetch($statement)) {
$entry = array();
$entry["success"] = true;
$entry["petname"] = $petname;
$entry["petbreed"] = $petbreed;
$entry["phonenumber"] = $phonenumber;
$entry["petdescription"] = $petdescription;
$entry["imageurl"] = $imageurl;
$response[] = $entry;
}

此外,您应该使用 echo json_encode($response); 而不是 print_r(json_encode($response));

在 Java 中,从现在起您将获得一个 JSON 数组作为响应。您需要更改:

JSONObject jsonResponse = new JSONObject(response);

至:

JSONArray jsonResponse = new JSONArray(response);

并循环遍历该数组以处理所有结果。

for(int i = 0; i < jsonResponse.length(); i++){
JSONObject obj = jsonResponse.getJSONObject(i);
petsList.add(new Pet(obj.getString("petname"), obj.getString("petbreed"),....));
}

关于java - 使用 php 和 android 从 sql 数据库中选择所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37372283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com