我正在使用 Volley 编写一个简单的Android应用程序。我想知道如何通过ID从MySQL数据库中选择名字和姓氏,哪个用户在应用程序中输入editText 。这是我的PHP脚本:
<?php
include 'connection.php';
global $connect;
$id = $_POST["id"];
$query = "SELECT firstName, lastName FROM users WHERE id = '$id'";
$result = mysqli_query($connect, $query);
$number_of_rows = mysqli_num_rows($result);
$response = array();
if($number_of_rows > 0) {
while($row = mysqli_fetch_assoc($result)) {
$response[] = $row;
}
}
header('Content-Type: application/json');
echo json_encode(array("users"=>$response));
mysqli_close($connect);
?>
如果我在代码中指定了ID,它会返回我要求的JSON数据,因此脚本和数据库都可以。我得到的$ id = 1的响应是:
{"users":[{"firstName":"Jonash","lastName":"Corvin"}]}
这是我的StringRequest代码:
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String firstName = jsonObject.getString("firstName");
String lastName = jsonObject.getString("lastName");
firstNameTV.setText(firstName);
lastNameTV.setText(lastName);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Something went wrong",Toast.LENGTH_LONG).show();
error.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> parameters = new HashMap<String, String>();
parameters.put("id", idEditText.getText().toString());
return parameters;
}
};
queue.add(stringRequest);
不幸的是,它没有做任何事情......它甚至没有显示任何错误或Toast消息。你知道怎么解决吗?
答案 0 :(得分:4)
{ “用户”:[{ “名字”: “Jonash”, “姓氏”: “Corvin酒店”}]}
根据给定的JSON,你需要像这样解析JSON:
JSONObject jsonobject = new JSONObject(response);
JSONArray jsonarray = jsonobject.getJSONArray("users");
JSONObject data = jsonArray.getJSONObject(0);
String firstName = data.getString("firstName");
String lastName = data.getString("lastName");
答案 1 :(得分:0)
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_TRANSACTION_DETAILS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
Log.i(TAG,response.toString());
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("read");
if (success.equals("1")) {
Toast.makeText(Activity.this,"Data Fetched!",Toast.LENGTH_SHORT).show();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String firstName= object.getString("firstName").trim();
String lastName= object.getString("lastName").trim();
}
}
catch (JSONException e) {
e.printStackTrace();
Toast.makeText(Activity.this,"Json Login Error " +e.toString(), Toast.LENGTH_SHORT).show();
}}},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Activity.this,"Volley Login Error " +error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("id", id);
return params;
}
};
答案 2 :(得分:-1)
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
$id = $_POST['id'];
require_once 'connection.php'
$sql = "SELECT firstName, lastName FROM users WHERE id='$id' ";
$response = mysqli_query($conn, $sql);
$result = array();
$result['read'] = array();
if( mysqli_num_rows($response) === 1 ) {
if ($row = mysqli_fetch_assoc($response)) {
$h['firstName'] = $row['firstName'] ;
$h['lastName'] = $row['lastName'] ;
array_push($result["read"], $h);
$result["success"] = "1";
echo json_encode($result);
}
}
}else {
$result["success"] = "0";
$result["message"] = "Error!";
echo json_encode($result);
mysqli_close($conn);
}
?>