当我需要一个响应时,我习惯使用Volley但是在这里,我能看到实现我想要的唯一方法就是使用2个响应。这是个主意:
从Android发布username
,在Php中获取匹配的user_id
,我喜欢这样:
<?php
require('dbConnect.php');
//this is me, my username in the user table
$Number = $_POST['phonenumberofuser'];
// get the username in the user table, then get the matching user_id
$query = "SELECT * FROM user WHERE username = ?";
$stmt = $con->prepare($query) or die(mysqli_error($con));
$stmt->bind_param('s', $Number) or die ("MySQLi-stmt binding failed ".$stmt->error);
$stmt->execute() or die ("MySQLi-stmt execute failed ".$stmt->error);
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
//get the corresponding user_id in the row
//this is the matching user_id in the user table of the user
$user_id = $row["user_id"];
}
然后在Php脚本的下一部分中,我想使用user_id
来获取user_id
创建的评论,如下所示:
$sql2 = "SELECT * FROM review WHERE user_id = '$user_id'";
$results = array();
$result2 = mysqli_query($con,$sql2);
//if user_id has reviews in the db
while($row = mysqli_fetch_array($result2)) {
//make an array called $results
$results[] = array(
'category' => $row['cat_name'],
'name' => $row['name'],
'phone' => $row['phone'],
'comment' => $row['comment'],
);
}
$json = json_encode($results);
echo $json;
?>
正如您所看到的,我的php文件与我的Android Volley代码所处理的$json
相呼应,在视图中为我提供了name
,phone
和comment
。 / p>
但是我在哪里将user_id
放入我的排球,所以我可以显示user_id
创建的相关评论?
这是我的Volley
代码:
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Review review = new Review();
review.setCategory(obj.getString("category"));
review.setName(obj.getString("name"));
review.setPhone(obj.getString("phone"));
review.setComment(obj.getString("comment"));
reviewList.add(review);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
答案 0 :(得分:1)
在第一个请求的onResponse
中添加新的截击请求的示例
// change JSONArrayrequest to your appropiate request to get the user id
JsonArrayRequest userReq = new JsonArrayRequest(URL_TO_GET_USERID,
new Response.Listener <JSONArray> () {
@Override
public void onResponse(JSONArray response) {
// parse your response to get the user_id
String user_id = <RESULT FROM PARSING YOUR RESPONSE>;
String URL_WITH_USERID = <YOUR URL> + user_id;
JsonArrayRequest movieReq = new JsonArrayRequest(URL_WITH_USERID,
new Response.Listener <JSONArray> () {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Review review = new Review();
review.setCategory(obj.getString("category"));
review.setName(obj.getString("name"));
review.setPhone(obj.getString("phone"));
review.setComment(obj.getString("comment"));
reviewList.add(review);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error geting user info: " + error.getMessage());
hidePDialog();
}
});
// Adding request for getting user info to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error getting user: " + error.getMessage());
hidePDialog();
}
});
// Adding request for getting user_id to request queue
AppController.getInstance().addToRequestQueue(userReq);