使用JsonArrayRequest传递POST参数

时间:2015-10-16 16:44:13

标签: php android mysql android-volley

我写了一个小文件,在数据库上运行搜索:

<?php$host = "localhost"; // host of MySQL server
$user = "root"; // MySQL user
$pwd = "root"; // MySQL user's password
$db = "android_api"; // database name

// Create connection
$con = mysqli_connect($host, $user, $pwd, $db);

// Check connection
if(mysqli_connect_errno($con)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}

$keyword = $_POST['keyword'];


// query the application data
$sql = "SELECT * FROM users WHERE name LIKE '%$keyword%' OR phone LIKE '%$keyword%'";
$result = mysqli_query($con, $sql);

// an array to save the application data
$rows = array();

// iterate to query result and add every rows into array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $rows[] = $row;
}

// close the database connection
mysqli_close($con);

// echo the application data in json format
echo json_encode($rows);

目前,它似乎正常运作。

现在,在我的Android应用程序中,我想使用Volley库调用JsonArrayRequest,然后使用结果填充Listview。我的问题是,它不是将关键字作为POST参数发送,所以我的服务器只是返回表中的每个用户。这是我的getResults()方法,它接收字符串'keyword':

private ArrayList<SearchResults> GetSearchResults(final String keyword)
    {

        pDialog.setMessage(getText(R.string.searching_dialog));
        pDialog.show();

        final ArrayList<SearchResults> results = new ArrayList<SearchResults>();

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, AppConfig.URL_SEARCH, new Response.Listener<JSONArray>() {
            public void onResponse(JSONArray jsonArray) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    try {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        SearchResults sr1 = new SearchResults();
                        sr1.setPhone(jsonObject.getString("phone"));
                        sr1.setName(jsonObject.getString("name"));
                        sr1.setSurname1(jsonObject.getString("surname1"));
                        sr1.setSurname2(jsonObject.getString("surname2"));
                        sr1.setEmail(jsonObject.getString("email"));
                        sr1.setImage(decodeBase64(jsonObject.getString("image")));
                        sr1.setUid(jsonObject.getString("unique_id"));
                        results.add(sr1);


                        //names.add(jsonObject.getString("name"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                //nameAdapter.notifyDataSetChanged();
                listAdapter.notifyDataSetChanged();

                if(results.isEmpty()){
                    Toast.makeText(getApplicationContext(),
                            R.string.no_search_results, Toast.LENGTH_LONG)
                            .show();
                }
                else
                {
                    welcomeText.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    pDialog.hide();
                }

            }
        }, new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("Error", "Unable to parse json array");
            }

        }        )
        {
            @Override
            protected Map<String, String> getParams(){
                Map<String, String> params = new HashMap<String, String>();
                params.put("keyword", keyword );

                return params;
            }
        };
        AppController.getInstance().addToRequestQueue(jsonArrayRequest);
        return results;
    }

有什么我想念的吗?为什么我的代码忽略了我发送的关键字?

由于

0 个答案:

没有答案