Android: Can not construct instance of java.sql.Ti

2019-05-18 17:16发布

问题:

I have developed a REST API and I am trying to connect to it using Android. Below is my code.

private void restCall()
    {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YourEndpoints request = retrofit.create(YourEndpoints.class);



        SetupBean setupBean = new SetupBean();
            setupBean.setIdPatient(1);
            setupBean.setCircleType(1);
            setupBean.setFamiliarity(1);
            setupBean.setValence(2);
            setupBean.setArousal(3);
            setupBean.setDateCreated(Common.getSQLCurrentTimeStamp());
            setupBean.setLastUpdated(Common.getSQLCurrentTimeStamp());

        Call<ResponseBody> yourResult = request.insertSetup(setupBean);
        yourResult.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                try {
                    Log.d("MainActivity", "RESPONSE: " + response.errorBody().string());
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                try {
                    t.printStackTrace();
                    Log.d("MainActivity", "RESPONSE: "+"FAILED");
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }

        });


    }

When I run this, it shows me the below error

Can not construct instance of java.sql.Timestamp from String value 'Jun 9, 2016 4:24:37 PM': not a valid representation (error: Failed to parse Date value 'Jun 9, 2016 4:24:37 PM': Can not parse date "Jun 9, 2016 4:24:37 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))

So it seems like there is an issue with converting the Timestamp. In my Bean class, below is how the Timestamp are defined.

public void setDateCreated(Timestamp dateCreated) {
        this.dateCreated = dateCreated;
    }

    public Timestamp getLastUpdated() {
        return lastUpdated;
    }

In my restCall() method, I am calling a Common.getSQLCurrentTimeStamp() to generate the timestamp. Below is that method.

public static Timestamp getSQLCurrentTimeStamp()
    {
        java.util.Date date = new java.util.Date();
        Timestamp t = new Timestamp(date.getTime());
        System.out.println(t);
        return t;
    }

So, when I run the restCall() method, why am I getting this Can not construct instance of java.sql.Timestamp from String value 'Jun 9, 2016 4:24:37 PM': not a valid representation (error: Failed to parse Date value 'Jun 9, 2016 4:24:37 PM': Can not parse date "Jun 9, 2016 4:24:37 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) error? How can I solve it?

回答1:

I fixed this by my self. Retrofit 2 is using GSON, so you have to give the date time converter manually

Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();