简体   繁体   中英

How can I post a parameter through JsonObjectRequest

I asked this before but I did not get a solution,

I am trying to post a parameter to the php script and get its results as json array however every time I try I get all of the columns in my table. At first I though there is something wrong with my php, however I tried from postman and see that my php works. So there is a problem in my code, problem is I am building a searchview so every time user enters it searches the data from database and shows the result in a listview. But I cannot post my parameter to my php script by using JsonObjectRequest. So how can I do it?

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);

}

here is my php code and I and I also changed the android code too

<?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);
}
?> 

You should override the " getParams() " method on your request object.

If it doesn't work for a JSONObjectRequest , then try using a StringRequest and override the getParams() method.

Here's an example from their 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;
        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM