简体   繁体   English

使用Android Volley Post请求从MySQL选择数据并在recyclerview中显示

[英]Select data from MySQL using android volley post request and display it in recyclerview

What I want is that when someone(user) click's a button on main activity two values will be send to the php file with the help of volley POST request, say minvalue and maxvalue, and it will select the data accordingly from mysql database. 我想要的是,当某人(用户)在主要活动上单击一个按钮时,将在凌空POST请求的帮助下将两个值(例如minvalue和maxvalue)发送到php文件,并且它将从mysql数据库中相应地选择数据。

Here's my code for POST request: 这是我的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 File Code: 我的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);

}
?>

When I try running my app using above code no data is selected and displayed in recyclerview. 当我尝试使用上述代码运行我的应用程序时,没有数据被选择并显示在recyclerview中。

But when I try Inserting values in the database using same variable then the values are inserted. 但是,当我尝试使用相同的变量在数据库中插入值时,将插入值。

I even tried keeping both select and insert statement to check whether it enters the "IF" loop, and yes it does enters and insert the values in DB but does not executes select statement. 我什至尝试同时保留select和insert语句来检查它是否进入“ IF”循环,是的,它确实输入并插入了DB中的值,但是不执行select语句。

However if I give an else statement to select all data and display it in recyclerview it works properly, so there's no problem in displaying the data in recyclerview the code works fine. 但是,如果我给出else语句以选择所有数据并在recyclerview中显示它,则它可以正常工作,因此在recyclerview中显示数据没有问题,代码可以正常工作。 Below is the same PHP code with else statement: 下面是带有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);
}

Please Help I don't know what I'm doing wrong or what I'm not doing. 请帮助我不知道我在做什么错或我没有做什么。

I don't see any problem in your code but check this if you get any output please inform me 我在您的代码中没有发现任何问题,但是请检查是否有任何输出,请通知我

<?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'];
 $sql="SELECT * FROM superinfo, publisher WHERE 
 superinfo.s_publisherid=publisher.p_id AND s_power BETWEEN '$minvalue' AND 
 '$maxvalue'";
 echo $sql;
 $result = mysqli_query($con,$sql);
 $output=array();
 $output = mysqli_fetch_assoc($result);
  print(json_encode($output));
  mysqli_close($con);
  }
  ?>

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

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