我正在研究一个项目,该项目通过HTTPS POST请求将数据导入Web服务。为了创建HTTPS请求,我使用Retrofit2。 这是我关于如何创建请求和评估响应的方法:
我已经尝试过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请求的正文而创建的改造库的调用:
@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);
}
}
这是我实现Callback接口的类:
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和向Webservices发出请求相对较新。预先谢谢您,我将非常感谢您的帮助
请求的响应应如下所示:
{
"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"
}
}
}
答案 0 :(得分:1)
您可以像下面那样更新pojo
getFromAsDate()
您可以从getToAsDate()
和{{1}}中获取对象的开始日期和结束日期