【问题标题】:Value <br of type java.lang.String cannot be converted to JSONObject because of a DATETIME data由于 DATETIME 数据,java.lang.String 类型的值 <br 无法转换为 JSONObject
【发布时间】:2017-04-24 06:31:04
【问题描述】:

我一直在努力尝试成功地将数据插入 MySQL 数据库(使用 Volley),这不是问题,因为数据已插入,但我一直遇到此错误 W/System.err: org.json.JSONException: Value
or an JSONEception, 这个错误停止做任何其他事情,如果我添加更多代码 UI 冻结,我试图从我的 php 文件中删除日期代码,一切正常,只是日期时间值不是我要查找的值,当我添加回来时,它再次向我显示错误。 这是我的代码:

    Response.Listener<String> responseListener1 = new Response.Listener<String>() {
                                                                    @Override
                                                                    public void onResponse(String response) {
                                                                        try {
                                                                            JSONObject jsonResponse1 = new JSONObject(response);
                                                                            boolean success = jsonResponse1.getBoolean("success");
                                                                            if (success) {


                                                                                Toast.makeText(MapActivity.this, "SUCCESS",Toast.LENGTH_LONG).show();


                                                                            } else {
                                                                                Toast.makeText(MapActivity.this, "INSERTION FAILED",Toast.LENGTH_LONG).show();


                                                                            }


                                                                        } catch (JSONException e) {
                                                                            e.printStackTrace();
                                                                            Toast.makeText(MapActivity.this, "EXCEPTION",Toast.LENGTH_LONG).show();
                                                                        }


                                                                    }
                                                                };




                                                                SendBookingRequest bookingRequest = new SendBookingRequest(idd,em,Adresse_source,duree,dist, responseListener1);
                                                                RequestQueue queue1 = Volley.newRequestQueue(MapActivity.this);
                                                                queue1.add(bookingRequest);

这是我的 php 文件 SendBookingRequest

<?php
require("password.php");
$connect = mysqli_connect("localhost", "XXXXX", "XXXXX", "XXXXX");

$driver_id = $_POST["driver_id"];
$email = $_POST["email"];
$adresse_source = $_POST["adresse_source"];
$duree = $_POST["duree"];
$distance = $_POST["distance"];
$response = array();
$dt_obj = new DateTime($response['send_moment'], new 
DateTimeZone('America/Chicago')); 
$dt_obj->setTimezone(new DateTimeZone('Europe/London')); 
$send_time = $dt_obj->format('Y-m-d H:i:s'); 
   echo $send_time;
 function AddRequest() {
    global $connect, $driver_id, $email, $adresse_source, $duree, $distance, $send_time ; 
    $statement = mysqli_prepare($connect, "INSERT INTO demande (driver_id, pass_id, adresse_source, duree, distance, send_moment) VALUES (?, (SELECT user_id FROM passager WHERE email = ?),?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "issdis", $driver_id, $email, $adresse_source, $duree, $distance, $send_time); 
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);     
}


    $response["success"] = false;  

    AddRequest();
    $response["success"] = true;  


echo json_encode($response);
?>

任何帮助将不胜感激。

【问题讨论】:

  • 展示数据样本的尴尬想法如何?
  • 对不起,你能解释一下你的意思吗?
  • 显示您尝试解析的导致您面临的问题的字符串
  • 怎么弄!!,对不起,我是新手
  • 通过记录?设置断点并使用调试器运行?

标签: php android mysql datetime android-volley


【解决方案1】:

我认为您需要将echo json_encode($response); 上方的header('Content-type: application/json'); 行放在php 代码中

【讨论】:

  • 我会添加它。当我与 Marcin Orlowski 讨论时,我发现错误的根源是未定义的索引 send_moment
猜你喜欢
  • 2013-08-02
  • 2023-03-10
  • 2021-10-23
  • 1970-01-01
  • 2020-04-02
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多