简体   繁体   English

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

[英]How to? Insert current date from Android into mySQL database using Retrofit2

Trying to insert Current date from android to mysql via Retrofit. 试图通过Retrofit将当前日期从android插入mysql。 The date is showing in the mysql database as 0000-00-00. 日期在mysql数据库中显示为0000-00-00。 I've read over multiple threads on SO on this topic and tried to apply the answers but so far I have not been successful. 我已经阅读了关于这个主题的SO上的多个主题,并尝试应用答案,但到目前为止我还没有成功。 Please see the below code snippets for context. 有关上下文,请参阅以下代码段。

PHP CODE: 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;
        }
 ?>

RETROFIT API CODE: 改造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 CODE: 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

I would like the date to insert succesfully and not show 0000-00-00 If I could also make the date show as DD-MM-YYYY that would be even better. 我希望日期成功插入而不是show 0000-00-00如果我还可以将日期显示为DD-MM-YYYY,那会更好。

This worked in the Java code, all other parts of the code stay the same 这适用于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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM