我想对存储在用于Android应用程序的远程数据库中的表进行自定义选择查询。 以下是我的php文件,该文件从POST方法获取表名,字段和条件并进行选择查询。然后,它返回获取的行的json对象。
<?php
require("config.php");
$tablename=$_POST['tablename'];
$condition=$_POST['condition'];
$data=json_decode($_POST['fields']);
$fields = implode(",",$data );
$sql="SELECT $fields FROM $tablename WHERE $condition";
if($res=mysqli_query($con,$sql))
{
while ( $row = $res->fetch_object()) { $myArray[ ] = $row; }
echo json_encode($myArray, JSON_FORCE_OBJECT);
}
else echo json_encode($sql." ".mysqli_error($con));
mysqli_close($con);
?>
我正在使用 POST 方法从我的android应用发送json对象请求,如下所示:
// Parameter Values:
String tablename = "teachers";
JSONArray fields = new JSONArray().put("password");
String condition = "teacher_id like \'" + id + "\'";
Map<String,String> params=new HashMap<>();
params.put("tablename",tablename);
params.put("fields",fields.toString());
params.put("condition",condition);
JSONObject jsonRequest = new JSONObject(params);
Toast.makeText(context, jsonRequest.toString(), Toast.LENGTH_SHORT).show();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST, DOMAIN+"/select.php", jsonRequest, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(context, "Success "+ response.toString(), Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
Toast.makeText(context, "Error"+error.getMessage(), Toast.LENGTH_LONG).show();
}
});
requestQueue.add(jsonObjectRequest);
json对象的格式正确,如下所示:
但是我仍然收到此错误:
有人可以帮助我获取选择查询输出的json对象吗?
更新:
我将php和java文件都更新为使用POST而不是GET。还是一样的错误。
更新:
我发出了此卷曲请求,打印出正确的结果
<?php
//The url you wish to send the POST request to
$url = "*****";
//The data you want to send via POST
$fields = [
'tablename' => 'teachers',
'fields' => '["password"]',
'condition' => 'teacher_id like \'MCA01\''
];
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
echo $result;
?>
输出: {“ 0”:{“ password”:“ 3438735f791 ********************** 8e7e5d24a5”}}
为什么来自android的请求不起作用?
答案 0 :(得分:0)
您无法在GET请求中发送JSON。
您要么不想发送任何JSON:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, DOMAIN+"/select.php", null, new Response.Listener<JSONObject>() {
或执行POST
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST, DOMAIN+"/select.php", jsonRequest, new Response.Listener<JSONObject>() {
实际上,您可以使用第三个构造函数,该构造函数可以让Volley库自行解决:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(DOMAIN+"/select.php", jsonRequest, new Response.Listener<JSONObject>() {
(并且因为您的JSON不为null,所以它将使用POST
)请参阅文档:https://afzaln.com/volley/com/android/volley/toolbox/JsonObjectRequest.html)
关于您编辑的问题,我认为问题在于,当请求期望响应为JSON时,您的服务器正在以HTML形式发送回响应。我可以使用断点来查看完整的错误是什么(以“