Android Volley - 发布请求 - 不向PHP发送参数

时间:2017-05-23 13:26:00

标签: php android android-volley

我试图通过Volley使用参数发出POST请求。它完全可以响应来自数据库IN MY ANDROID APP的查询结果,但当我在php中显示此查询的值时,则显示错误未定义的索引变量" uploaded_by"。

JAVA CODE:

 StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                    }
                },
                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(KEY_USERNAME,username);
                params.put(KEY_PASSWORD,password);
                params.put(KEY_EMAIL, email);
                return params;
            }

        };

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

  1. 航向

  2. PHP代码:

    <?php
    //This script is designed by Android-Examples.com
    //Define your host here.
    $servername = "localhost";
    //Define your database username here.
    $username = "root";
    //Define your database password here.
    $password = "";
    //Define your database name here.
    $dbname = "u288012116_and";
    
    $connection = new mysqli($servername,$username,$password,$dbname) ;
    
    // Innitialize Variable
    if($_SERVER['REQUEST_METHOD'] == 'POST')
     {
    
     $StudentData = $_POST['uploaded_by'];
    
    
    
    
              //fetch table rows from mysql db
    $sql = "SELECT image_title,image_url FROM  `imagelistviewtable` WHERE  `uploaded_by` = '". $StudentData . "'";
    
    
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
    
        //create an array
    $emparray = array();
    while($row = mysqli_fetch_assoc($result))
        {
            $emparray[] = $row;
        }
    
    
    $json = json_encode($emparray);
    if ($json === false) {
        // Avoid echo of empty string (which is invalid JSON), and
        // JSONify the error message instead:
        $json = json_encode(array("jsonError", json_last_error_msg()));
        if ($json === false) {
            // This should not happen, but we go all the way now:
            $json = '{"jsonError": "unknown"}';
        }
        // Set HTTP response status code to: 500 - Internal Server Error
        http_response_code(500);
    }
    
    echo $json;
    //close the db connection
        mysqli_close($connection);
     }
    
        ?>
    

1 个答案:

答案 0 :(得分:0)

 public static void getServerData(final Context cntx) {
        RequestQueue queue = Volley.newRequestQueue(cntx);


        JsonObjectRequest jsObjRequest = new JsonObjectRequest(
                Request.Method.POST, "Url", json_object,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {

                         // Your response

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub

                    }

                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {

                // Your header parameter if you have otherwise this is optional

                return headers;
            }
        };
   queue.add(jsObjRequest);
}