尝试通过 Retrofit 将当前日期从 android 插入 mysql。日期在 mysql 数据库中显示为 0000-00-00。我已经阅读了关于这个主题的多个线程并尝试应用答案,但到目前为止我还没有成功。请参阅下面的代码片段了解上下文。
PHP代码:
<?php
include('conn.php');
$userId = $_POST['userId'];
$moodBefore = $_POST['moodBefore'];
$automaticThought = $_POST['automaticThought'];
$distortions = $_POST['distortions'];
$challengeThought = $_POST['challengeThought'];
$alternativeThought = $_POST['alternativeThought'];
$moodAfter = $_POST['moodAfter'];
$posted = $_POST['posted'];
$insert = "INSERT INTO Cbt ( userId, moodBefore, automaticThought, distortions, challengeThought, alternativeThought, moodAfter, posted) VALUES
('$userId','$moodBefore', '$automaticThought', '$distortions', '$challengeThought', '$alternativeThought', '$moodAfter', '$posted')";
$resultinsert = $conn -> query($insert);
if(!$resultinsert){
echo $conn->error;
}
?>
改造 API 代码:
public interface Api {
@FormUrlEncoded
@POST("insert.php")
Call<ResponseBody> insertLog(
@Field("userId") int userId,
@Field("moodBefore") int moodBefore,
@Field("automaticThought") String automaticThought,
@Field("distortions") int distortions,
@Field("challengeThought") String challengeThought,
@Field("alternativeThought") String alternativeThought,
@Field("moodAfter") int moodAfter,
@Field("posted") String date
);
JAVA代码:
String posted = new SimpleDateFormat("ddMMyyyy").format(new Date());
case R.id.nextBtnMoodPage:
if (hasSelected) {
Call<ResponseBody> call = RetrofitClient
.getInstance()
.getApi()
.insertLog(userId, moodBefore, automaticThoughtString, distortions, challengeThoughtString, alternativeThoughtString, moodAfter, posted);
call.enqueue(new Callback<ResponseBody>() {.....and so on
我希望成功插入日期而不显示 0000-00-00 如果我也可以将日期显示为 DD-MM-YYYY 那会更好。