gpt4 book ai didi

java - 如何?使用 Retrofit2 将当前日期从 Android 插入到 mySQL 数据库中

转载 作者:行者123 更新时间:2023-11-29 15:59:20 24 4
gpt4 key购买 nike

尝试通过 Retrofit 将当前日期从 android 插入到 mysql。日期在 mysql 数据库中显示为 0000-00-00。我已经阅读了有关此主题的多个线程,并尝试应用答案,但到目前为止我还没有成功。请参阅下面的代码 fragment 了解上下文。

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 那就更好了。

最佳答案

这在 Java 代码中有效,代码的所有其他部分保持不变

private Date currentDate() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 0);
return cal.getTime();
}

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
String posted = dateFormat.format(currentDate());

关于java - 如何?使用 Retrofit2 将当前日期从 Android 插入到 mySQL 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56313128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com