之前我问过这个,但我没有得到解决方案,
我正在尝试将参数发布到php脚本并将其结果作为json数组获取但是每次我尝试时都会获得表格中的所有列。起初我虽然我的PHP有问题,但是我尝试从postman看到我的php工作。所以我的代码存在问题,问题是我正在构建一个searchview,所以每次用户输入它时都会搜索数据库中的数据并在listview中显示结果。但我不能通过使用JsonObjectRequest将我的参数发布到我的PHP脚本。那我怎么能这样做呢?
public void findSearchedUsers(String s)
{
HashMap<String, String> params = new HashMap<String, String>();
params.put("keyword",s );
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,"http://ksdb.comlu.com/search.php", new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray= response.getJSONArray("users");
for(int i = 0; i < jsonArray.length(); i ++){
JSONObject user = jsonArray.getJSONObject(i);
String id = user.getString("u_id");
String name = user.getString("u_name");
String surname = user.getString("u_lname");
String email = user.getString("u_email");
String password = user.getString("u_pw");
String department = user.getString("u_dp");
User newUser = new User(id, name, surname, email, password, department);
userArrayList.add(newUser);
}
setUsersListView(userArrayList );
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
});
RequestQueue queue = Volley.newRequestQueue(MainUserPage.this);
queue.add(req);
}
这是我的PHP代码,我和我也改变了Android代码
<?php
// include connect class
$response = array();
// include connect class
require_once 'connect.php';
// connecting to db
$db = new DB_CONNECT();
// connecting to db
$keyword=$_GET["keyword"];
$result = mysql_query("SELECT * FROM user WHERE u_name LIKE'%$keyword%' LIMIT 0, 20")
or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
$response["users"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$users= array();
$users["u_id"] = $row["u_id"];
$users["u_name"] = $row["u_name"];
$users["u_lname"] = $row["u_lname"];
$users["u_email"] = $row["u_email"];
$users["u_pw"] = $row["u_pw"];
$users["u_dp"] = $row["u_dp"];
array_push($response["users"], $users);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No idioms found";
// echo no users JSON
echo json_encode($response);
}
?>
答案 0 :(得分:0)
您应该覆盖请求对象上的“getParams()
”方法。
如果它不适用于JSONObjectRequest
,请尝试使用StringRequest
并覆盖getParams()
方法。
答案 1 :(得分:0)
以下是他们help的一个例子。
StringRequest sr = new StringRequest(Request.Method.POST,"http://api.someservice.com/post/comment", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mPostCommentResponse.requestCompleted();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mPostCommentResponse.requestEndedWithError(error);
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user",userAccount.getUsername());
params.put("pass",userAccount.getPassword());
params.put("comment", Uri.encode(comment));
params.put("comment_post_ID",String.valueOf(postId));
params.put("blogId",String.valueOf(blogId));
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};