【问题标题】:Error in sending and recieving json object from Android to php using JsonObjectRequest method使用 JsonObjectRequest 方法从 Android 向 php 发送和接收 json 对象时出错
【发布时间】:2019-11-17 10:51:15
【问题描述】:

我想对存储在远程数据库中的 android 应用程序的表进行自定义选择查询。 以下是我的 php 文件,它从 POST 方法中获取 tablename, fields 和 condition 并进行选择查询。然后它返回获取的行的 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 对象的格式正确,如下所示:

但我仍然收到此错误:

谁能帮我获取select查询输出的json对象?

更新:

我更新了 php 和 java 文件以使用 POST 而不是 GET。还是一样的错误。

更新:

我提出了打印正确结果的 curl 请求

<?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":{"密码":"3438735f791**********************8e7e5d24a5"}}

为什么来自android的请求不起作用?

【问题讨论】:

  • 您的代码易受 SQL 注入攻击。
  • 您能详细说明或推荐任何修复方法吗?
  • 我已经更新了代码。有一些进展。我认为问题在于字段变量中的转义序列
  • 尽管如此,通过 Internet 将 SQL 发送到 PHP 是一个糟糕的主意。任何人都可以发送他们想要的任何东西。

标签: php android sql json


【解决方案1】:

您不能在 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 不为空,它将使用 POST)请参阅文档:https://afzaln.com/volley/com/android/volley/toolbox/JsonObjectRequest.html)


对于您编辑过的问题,我认为现在的问题是您的服务器在请求期望响应为 JSON 时以 HTML 格式发回响应。我要么断点,看看完整的错误是什么(开始“

【讨论】:

  • 感谢您的回复。我更新了 php 和 java 文件以使用 POST 而不是 GET。但我仍然得到同样的错误。请查看更新后的代码
  • 我尝试调试 php 文件。错误在于密码字段变量。未正确解析 json 数组。你能帮我修一下吗?
  • 对不起 PHP 不是我的强项,但我确实回答了您的问题,如果您记得将其标记为已回答请。我建议针对您的 PHP 问题提出一个新问题
猜你喜欢
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 2017-02-07
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多