How to fix 'JsonSyntaxException' at Date conversion

Matthäus Schwarzkogler :

I am working on a Project which imports data into a Webservice via HTTPS POST Requests. In order to create HTTPS Requests I am using Retrofit2. This is my approach on how to create the request and evaluate the response:

  1. I am creating an Object where I store my information
  2. I call a method of the Object which converts the data into a proper format for the request (JSON Format)
  3. I create the Retrofit Client and make a call.
  4. The last point is evaluating the Response (at this point it skips the onResponse() method and enters the onFailure() method with JsonSyntaxException

I've already tried a DateDeserializer... Unfortunately without any results

This is the format of the Request:

tokenid=ABCDEFG
&bookuser=testuser
&input= {
    "0": {
        "ISSUEKEY": "ABC-81",
        "STARTDATE": "15.12.2016 09:00",
        "ENDDATE": "15.12.2016 11:30",
        "BOOKINGTEXT": "Testeintrag 1. Stephan Perner ISSUEKEY, ohne Sekunden"
    },
    "1": {
        "ISSUEKEY": "ABC-82",
        "STARTDATE": "15.12.2016 12:00",
        "ENDDATE": "15.12.2016 17:45",
        "BOOKINGTEXT": "Testeintrag 2. Stephan Perner, ohne Sekunden"
    }
}

This is the method which will be called when I press a Button in order to start the sending process:


        w.setBookingtext("testeintrag#2");
        System.out.println("String: " + w.toRequest());
        String token = loadToken();
        Log.e(TAG, token);

        Call call = jiraService.postWorktime(token, "mschwar", "{\"0\":" + w.toRequest() + "}");

This is the Call of the Retrofit Library which I created in order to fill the Body of the POST Request:

    @POST("postWorktime")
    Call<PostWorkTimeResponse> postWorktime(@Field("tokenid") String token, @Field("bookuser") String bookuser, @Field("input") String input);

This is the POJO:

import com.google.gson.annotations.SerializedName;

import org.json.JSONObject;

import java.sql.SQLOutput;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Worktime
{

    @SerializedName("ISSUEKEY")
    private String projectKey;
    @SerializedName("STARTDATE")
    private Date from;
    @SerializedName("ENDDATE")
    private Date to;
    @SerializedName("BOOKINGTEXT")
    private String bookingtext;

    public Worktime(Date from, Date to, String projectKey) {
        this.from = from;
        this.to = to;
        this.projectKey = projectKey;
    }

    public Date getFrom() {
        return from;
    }

    public void setFrom(Date from) {
        this.from = from;
    }

    public Date getTo() {
        return to;
    }

    public void setTo(Date to) {
        this.to = to;
    }

    public String getProjectKey() {
        return projectKey;
    }

    public void setProjectKey(String projectKey) {
        this.projectKey = projectKey;
    }

    public String getBookingtext() {
        return bookingtext;
    }

    public void setBookingtext(String bookingtext) {
        this.bookingtext = bookingtext;
    }

    @Override
    public String toString() {
        return "Worktime{" +
                "from=" + from +
                ", to=" + to +
                ", projectKey='" + projectKey + '\'' +
                ", bookingtext='" + bookingtext + '\'' +
                '}';
    }

    public String toRequest()
    {
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.GERMANY);
        Date dStart = from;
        Date dEnd = to;
        dEnd.setHours(15);
        return String.format("{" +
                        "\"ISSUEKEY\":\"%s\"," +
                        "\"STARTDATE\":\"%s\"," +
                        "\"ENDDATE\":\"%s\"," +
                "\"BOOKINGTEXT\":\"%s\"" +
                               "}", projectKey, df.format(dStart), df.format(dEnd), bookingtext);
    }
}

And this is the class where I implemented the Callback interface:

package com.ssi.zeiterfassungsapp.webservice;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ssi.zeiterfassungsapp.beans.PostWorkTimeRelated.PostWorkTimeResponse;
import com.ssi.zeiterfassungsapp.util.AsyncDelegate;

import java.lang.reflect.Type;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WorktimeImporter implements Callback
{
    private static final String TAG = "WorktimeImporter";
    private Context mContext;
    private AsyncDelegate delegate;
    private Gson gson;

    public WorktimeImporter(Context mContext, AsyncDelegate delegate) {
        this.mContext = mContext;
        this.delegate = delegate;
        gson = new GsonBuilder().setLenient().serializeNulls().setDateFormat("dd.MM.yyyy HH:mm").create();
    }

    @Override
    public void onResponse(Call call, Response response) {
        Type type = new TypeToken<PostWorkTimeResponse>(){}.getType(); //Creates instance of the Type (PostWorkTimeResponse)
        PostWorkTimeResponse workTimeResponse = null;
        Log.e(TAG, "Error in onResponse()");
        if(response.code() == 200){
            workTimeResponse = (PostWorkTimeResponse) response.body();

        }else
        {
            try {
                workTimeResponse = gson.fromJson(response.errorBody().string(), type); //Converts the response to LoginResponse
                Log.e(TAG, response.errorBody().string());
            } catch (Exception e) {
                Log.e(TAG, "Error: " + e.getMessage());
            }
        }
        Log.e(TAG, workTimeResponse.toString());
    }

    @Override
    public void onFailure(Call call, Throwable t) {
        t.printStackTrace();
    }
}

And this is the result of printStackTrace():

W/System.err: com.google.gson.JsonSyntaxException: 15.07.2019 12:48
        at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:74)
        at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:59)
        at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:41)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
        at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
        at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
        at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
W/System.err:     at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117)
        at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
        at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
W/System.err: Caused by: java.text.ParseException: Failed to parse date ["15.07.2019 12:48']: Invalid number: 15.0
        at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274)
        at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:72)
        ... 18 more
    Caused by: java.lang.NumberFormatException: Invalid number: 15.0
        at com.google.gson.internal.bind.util.ISO8601Utils.parseInt(ISO8601Utils.java:318)
        at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:129)
        ... 19 more

I hope somebody can help me with this issue, I am relatively new to Retrofit and making Requests to Webservices. Thank you in advance, I would really appreciate some help

The response of the Request should look like that:

{
"status": "Ok",
"token": "ABCDEFG",
"user": "testuser",
"origin": "XXX",
"target": "IF",
"bookuser": "testuser",
"booking": 
{
0: 
{
"ISSUEKEY": "ABC-81",
"STARTDATE": "15.12.2016 09:00",
"ENDDATE": "15.12.2016 11:30",
"BOOKINGTEXT": "Testeintrag 1. Stephan Perner auf korrekten ISSUEKEY, ohne Sekunden"
}
,
1: 
{
"ISSUEKEY": "ABC-82",
"STARTDATE": "15.12.2016 12:00",
"ENDDATE": "15.12.2016 17:45",
"BOOKINGTEXT": "Testeintrag 2. Stephan Perner auf korrekten ISSUEKEY, ohne Sekunden"
}
}
}
Rajasekaran M :

You can update your pojo like below

public class Worktime
{

    @SerializedName("ISSUEKEY")
    private String projectKey;
    @SerializedName("STARTDATE")
    private String from;
    @SerializedName("ENDDATE")
    private String to;
    @SerializedName("BOOKINGTEXT")
    private String bookingtext;

    public Worktime(String from, String to, String projectKey) {
        this.from = from;
        this.to = to;
        this.projectKey = projectKey;
    }

    public String getFrom() {
        return from;
    }

    public String getFromAsDate() {
       try {
        return new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.GERMANY).parse(from);
       } catch (Exception e) {

        }
        return null;
    }

    public void setFrom(Date from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public String getToAsDate() {
       try {
        return new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.GERMANY).parse(to);
       } catch (Exception e) {

        }
        return null;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getProjectKey() {
        return projectKey;
    }

    public void setProjectKey(String projectKey) {
        this.projectKey = projectKey;
    }

    public String getBookingtext() {
        return bookingtext;
    }

    public void setBookingtext(String bookingtext) {
        this.bookingtext = bookingtext;
    }

    @Override
    public String toString() {
        return "Worktime{" +
                "from=" + from +
                ", to=" + to +
                ", projectKey='" + projectKey + '\'' +
                ", bookingtext='" + bookingtext + '\'' +
                '}';
    }

    public String toRequest()
    {
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.GERMANY);
        Date dStart = from;
        Date dEnd = to;
        dEnd.setHours(15);
        return String.format("{" +
                        "\"ISSUEKEY\":\"%s\"," +
                        "\"STARTDATE\":\"%s\"," +
                        "\"ENDDATE\":\"%s\"," +
                "\"BOOKINGTEXT\":\"%s\"" +
                               "}", projectKey, df.format(dStart), df.format(dEnd), bookingtext);
    }
}

You can get startdate and enddate as object from getFromAsDate() and getToAsDate()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=155063&siteId=1