gpt4 book ai didi

java - 如何在日期转换时修复 'JsonSyntaxException'

转载 作者:行者123 更新时间:2023-11-30 10:02:17 25 4
gpt4 key购买 nike

我正在开发一个通过 HTTPS POST 请求将数据导入 Web 服务的项目。为了创建 HTTPS 请求,我使用了 Retrofit2。这是我关于如何创建请求和评估响应的方法:

  1. 我正在创建一个对象来存储我的信息
  2. 我调用对象的方法将数据转换为适合请求的格式(JSON 格式)
  3. 我创建了 Retrofit Client 并进行了调用。
  4. 最后一点是评估响应(此时它跳过 onResponse() 方法并进入带有 JsonSyntaxException 的 onFailure() 方法

我已经尝试过 DateDeserializer...不幸的是没有任何结果

这是请求的格式:

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"
}
}

这是当我按下按钮以开始发送过程时将调用的方法:


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() + "}");

这是我为了填充 POST 请求的主体而创建的 Retrofit 库的调用:

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

这是 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);
}
}

这是我实现回调接口(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();
}
}

这是 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

我希望有人能帮助我解决这个问题,我对 Retrofit 和向 Web 服务发出请求还比较陌生。提前谢谢你,我真的很感激一些帮助

请求的响应应该是这样的:

{
"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"
}
}
}

最佳答案

您可以像下面这样更新您的 pojo

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

您可以从 getFromAsDate()getToAsDate() 获取开始日期和结束日期作为对象

关于java - 如何在日期转换时修复 'JsonSyntaxException',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57040132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com