2

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()));

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.