我试图在我的应用程序中实现数据库搜索功能,该功能搜索用户表并返回类似搜索字符串的用户数组。我需要这个数组来填充下降到acTextView下面的列表,但是我无法正常传递JSONArray,而且我不确定我是如何将数据传递给acTextView的。任何有关这方面的帮助将不胜感激!
应用程序端搜索活动:
public class PopupAddContact extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup_add_contact);
getWindow().setLayout(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
final AutoCompleteTextView searchInput = (AutoCompleteTextView) findViewById(R.id.searchText);
searchInput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
final String name = searchInput.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
System.out.println(response);
//ArrayList<String> array = new ArrayList<String>();
JSONArray jsonResponse = new JSONArray(response);
System.out.println(jsonResponse);
System.out.println("working");
/*
if (jsonResponse != null) {
Integer contact_user_id = jsonResponse.getInt();
String contact_name = jsonResponse.getString("name");
String contact_email = jsonResponse.getString("email");
} else if(response != null){
Toast.makeText(PopupAddContact.this, "No users found", Toast.LENGTH_SHORT).show();
} */
} catch (JSONException e) {
e.printStackTrace();
}
}
};
PopupContactRequest AddContactRequest = new PopupContactRequest(name, responseListener);
RequestQueue queue = Volley.newRequestQueue(PopupAddContact.this);
queue.add(AddContactRequest);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
用于检索用户数组的PHP脚本:
if(!empty($_POST["name"])){ // check post data and then start
$mysqli = new mysqli("db creds");
$name = $_POST["name"];
$response=array();
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT user_id, name, email FROM users WHERE name LIKE %?%";
if ($stmt = $mysqli->prepare($query)) { // prepare query
$stmt->bind_param("s", $name); // bind parameters
$stmt->execute();
$stmt->bind_result($data);
while ($stmt->fetch()) {
$response[] = $data; //assign each data to response array
}
}
echo json_encode($response);
$mysqli->close();
}else{
echo "no users found";
}
答案 0 :(得分:0)
我认为你的php文件没有回显有效的json,请尝试更改这一行:
if ($stmt = $mysqli->prepare($query)) { // prepare query
$stmt->bind_param("s", $name); // bind parameters
$stmt->execute();
$stmt->bind_result($user_id, $name, $email);
while ($stmt->fetch()) {
$response[] = [
"user_id" => $user_id,
"name" => $name,
"email" => $email,
]; //assign each data to response array
}
}
echo json_encode(array_values($response));
首先你应该调用php网站并在https://jsonlint.com/之类的json验证器中验证结果,然后你可以继续使用java代码。
在回显json响应之前编写json标头也是一种很好的做法:
header('Content-Type: application/json');
echo json_encode($response);