使用android volley post请求从MySQL中选择数据并将其显示在recyclerview中

时间:2016-04-30 09:08:05

标签: php mysql json post android-volley

我想要的是,当某人(用户)点击主活动上的按钮时,将在volley POST请求的帮助下将两个值发送到php文件,例如minvalue和maxvalue,它将相应地从mysql中选择数据数据库中。

这是我的POST请求代码:

public void FilterPower(View view) {

    StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.DATA_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    /*Some Method*/
                    /*Display data in recyclerview*/

                    //Initializing Views
                    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
                    recyclerView.setHasFixedSize(true);
                    layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(layoutManager);

                    //Initializing our superheroes list
                    listSuperHeroes = new ArrayList<>();

                    //Calling method to get data
                    getData();

                    /*End Display data in recyclerview*/
                    /*End Some Method*/

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {

        @Override
        protected Map<String,String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

            params.put("minvalue", "1000");
            params.put("maxvalue", "2000");
            return params;
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    requestQueue.add(stringRequest);

}

}

My Php文件代码:

<?php

$con = mysqli_connect("127.0.0.1","superinfoadmin","","superhero");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

if($_SERVER['REQUEST_METHOD']=='POST'){

$minvalue       = $_POST['minvalue'];
$maxvalue       = $_POST['maxvalue'];

$result = mysqli_query($con, "SELECT * FROM superinfo, publisher WHERE superinfo.s_publisherid=publisher.p_id AND (s_power BETWEEN $minvalue AND $maxvalue)");

//$result = mysqli_query($con, "INSERT INTO testDB (min, max) VALUES ('$minvalue', '$maxvalue')");

while($row = mysqli_fetch_assoc($result))
{
    $output[]=$row;
}
print(json_encode($output));

mysqli_close($con);

}
?>

当我尝试使用上面的代码运行我的应用程序时,没有数据被选中并显示在recyclerview中。

但是当我尝试使用相同的变量在数据库中插入值时,会插入值。

我甚至尝试同时使用select和insert语句来检查它是否进入“IF”循环,是的,它确实输入并在DB中插入值但不执行select语句。

但是如果我给一个else语句来选择所有数据并在recyclerview中显示它可以正常工作,那么在recyclerview中显示数据没有问题,代码工作正常。下面是与else语句相同的PHP代码:

if($_SERVER['REQUEST_METHOD']=='POST'){

$minvalue       = $_POST['minvalue'];
$maxvalue       = $_POST['maxvalue'];

$result = mysqli_query($con, "SELECT * FROM superinfo, publisher WHERE superinfo.s_publisherid=publisher.p_id AND (s_power BETWEEN $minvalue AND $maxvalue)");

//$result = mysqli_query($con, "INSERT INTO testDB (min, max) VALUES ('$minvalue', '$maxvalue')");

while($row = mysqli_fetch_assoc($result))
{
    $output[]=$row;
}
print(json_encode($output));

mysqli_close($con);

}
else{

$result = mysqli_query($con, "SELECT * FROM superinfo, publisher WHERE superinfo.s_publisherid=publisher.p_id");

while($row = mysqli_fetch_assoc($result))
{
    $output[]=$row;
}
print(json_encode($output));

mysqli_close($con);
}

请帮助我不知道我做错了什么或我不做什么。

1 个答案:

答案 0 :(得分:0)

我的代码中没有任何问题,但如果您有任何输出请检查一下,请告知我

ANativeWindow