dot1mav
dot1mav

Reputation: 48

java.util.concurrent.ExecutionException: com.google.gson.JsonSyntaxException: 2020-01-15 15:13:42.0

I send data with Gson and convert the JSON to object with Gson Library.

JSON Data -

{
   "map":{
      "date":"2020-01-15 15:13:42.0",
      "botType":1,
      "botName":"ds",
      "id":62,
      "userId":1,
      "accountKey":"dcab171a-b6cc-4583-b5fc-3e996100725a",
      "status":0
   }
}

and data Convert class is :

public class DateConverter {
    @TypeConverter
    public static Date toDate(Long timestamp) {
        return timestamp == null ? null : new Date(timestamp);
    }

    @TypeConverter
    public static Long toTimestamp(Date date) {
        return date == null ? null : date.getTime();
    }
}

I have problem with get data from server in android and error like that :

01-27 15:47:00.506 2070-2070/xxx.xx.xxxxx.android W/System.err: java.util.concurrent.ExecutionException: com.google.gson.JsonSyntaxException: 2020-01-15 15:13:42.0
01-27 15:47:00.506 2070-2070/xxx.xx.xxxxx.android W/System.err:     at java.util.concurrent.FutureTask.report(FutureTask.java:93)
01-27 15:47:00.506 2070-2070/xxx.xx.xxxxx.android W/System.err:     at java.util.concurrent.FutureTask.get(FutureTask.java:163)
01-27 15:47:00.506 2070-2070/xxx.xx.xxxxx.android W/System.err:     at android.os.AsyncTask.get(AsyncTask.java:483)

the sender Code :

objectOutputStream = new ObjectOutputStream(response.getOutputStream());
                    objectOutputStream.writeObject(new Gson().toJson((JSONObject) messageForClient.getT()));

Upvotes: 2

Views: 403

Answers (2)

Farhood Naqizade
Farhood Naqizade

Reputation: 116

Use this :

new GsonBuilder().setDateFormat("yyyy-MM-DD HH:mm:ss").create().fromJson(string , YourObject.class);

Your Format is : "yyyy-MM-DD HH:mm:ss"

Upvotes: 1

Adir Dayan
Adir Dayan

Reputation: 1617

When you creating the Gson instance, define the Date format.

For example:

Gson gson = new GsonBuilder()
   .setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").create();

Try to modify the string format to match your date syntax.

Upvotes: 1

Related Questions