[英]How to? Insert current date from Android into mySQL database using Retrofit2
试图通过Retrofit将当前日期从android插入mysql。 日期在mysql数据库中显示为0000-00-00。 我已经阅读了关于这个主题的SO上的多个主题,并尝试应用答案,但到目前为止我还没有成功。 有关上下文,请参阅以下代码段。
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
我希望日期成功插入而不是show 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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.