繁体   English   中英

我如何通过JsonObjectRequest发布参数

[英]How can I post a parameter through JsonObjectRequest

我之前曾问过这个问题,但没有得到解决方案,

我正在尝试将参数发布到php脚本,并以json数组的形式获取其结果,但是每次我尝试获取表中的所有列。 起初我虽然php出了点问题,但是我从邮递员那里尝试过,发现我的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);
}
?> 

您应该在请求对象上覆盖“ getParams() ”方法。

如果它不适用于JSONObjectRequest ,则尝试使用StringRequest并重写getParams()方法。

这是他们帮助下的一个例子。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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