Android [改装] - 不可解决的日期

时间:2018-05-28 21:53:50

标签: android json parsing retrofit

我通过Retrofit发送GET请求,我的问题如下:

io.reactivex.exceptions.OnErrorNotImplementedException: java.text.ParseException: Unparseable date: "2018-05-09"

我的对象模型:

public class Device {
    private int id;
    private int phoneNumber;
    private String connectionName;
    private String deviceName;
    private Date startConnection;
    private Location actualLocation;
    private User owner;

//getters setters and constructor
}

GET:

public void getAllDevicesForCurrentUser(){
        compositeDisposable.add(restAPI.getDevices("Bearer " + TokenHolder.getInstance().getToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<Device>>() {
                    @Override
                    public void accept(List<Device> devices) throws Exception {
                        initializeDevices(devices);
                    }
                }));
    }

有什么问题?

3 个答案:

答案 0 :(得分:0)

您必须在改装初始化中设置日期格式。这是你怎么做的

Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd")
.create();

Retrofit retrofitAdapter = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

答案 1 :(得分:0)

好的,我解决了这个问题。

我在对象中的所有变量上面添加@Expose,在变量中添加Expose(deserialized = false),我不需要从JSON解析。

最后,

Gson gson = new GsonBuilder()
                    .excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();

答案 2 :(得分:-1)

现在我得到了

 io.reactivex.exceptions.OnErrorNotImplementedException: java.text.ParseException: Unparseable date: "00:06:00"

我想解析我的对象位置actualLocation

public class Device {
    private int id;
    private int phoneNumber;
    private String connectionName;
    private String deviceName;
    private Date startConnection;
    private Location actualLocation;
    private User owner;

//getters setters and constructor
}

和位置:

public class Location {
    private int id;
    private float gpsLatitude;
    private float gpsLongitude;
    private Date date;
    private Time time;
    private Device device;
}