[英]Android Volley - Post Request - Not Sending parameter to php
I was trying to make a POST request with parameter through Volley.我试图通过 Volley 发出带有参数的 POST 请求。 It is working totally fine to response query result from database IN MY ANDROID APP but when i show the values of this query in php the then show error undefined index variable "uploaded_by".
从我的ANDROID APP中的数据库响应查询结果完全可以正常工作,但是当我在php中显示此查询的值时,然后显示错误未定义的索引变量“uploaded_by”。
JAVA CODE: JAVA代码:
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);
}
Heading标题
PHP CODE: 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);
}
?>
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);
}
I'm a bit late to answer this, but I think it may help someone facing same problem.我回答这个问题有点晚了,但我认为它可能会帮助面临同样问题的人。 I search through all similar questions on SO, relating to PHP and Volley api but I didn't find any satisfying answer.
我搜索了关于 SO 的所有类似问题,与 PHP 和 Volley api 有关,但我没有找到任何令人满意的答案。
The problem is that you are sending data from the Volley library as content type问题是您将 Volley 库中的数据作为内容类型发送
application/json
应用程序/json
but your PHP script is expecting POST data as content type但是您的 PHP 脚本期望 POST 数据作为内容类型
application/x-www-form-urlencoded
应用程序/x-www-form-urlencoded
In your PHP script, do this:在您的 PHP 脚本中,执行以下操作:
$_POST = json_decode(file_get_contents('php://input'), true);
if ( !empty($_POST['uploaded_by']) ) {
$StudentData = $_POST['uploaded_by'];
echo $StudentData;
}
Now if you check for现在如果你检查
if( isset($_POST['uploaded_by']) ){
echo "something";
}
it should work now它现在应该可以工作了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.