如何?使用Retrofit2将Android中的当前日期插入MySQL数据库

时间:2019-05-26 11:36:22

标签: java php android mysql retrofit2

试图通过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;
        }
 ?>

RETROFIT 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,那会更好。

1 个答案:

答案 0 :(得分:0)

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

divobj.innerHTML = "Your Total: £" + TotalpkgPrice.toFixed(2)  ;