我在运行API 26的仿真器实例与运行API 28的仿真器实例之间,以下代码段的工作方式不同。这是我正在运行的将GPS点数据对象转换为JSON的代码段。
GPSPointDataFinal dataFinal;
// bunch of code to put data into dataFinal
Gson gson = new GsonBuilder()
.serializeNulls()
.create();
String jsonFinalData = gson.toJson(dataFinal);
当我在API 26仿真器中运行此代码部分时,我得到以下JSON输出:
{"gpsData":[
{"elevation":0.0,"location":{
mAltitude":0.0,"mBearing":122.0,"mBearingAccuracyDegrees":0.0,"mElapsedRealtimeNanos":70951742958220,"mExtras":null,"mFieldsMask":14,"mHorizontalAccuracyMeters":20.0,"mLatitude":38.9264983,"mLongitude":-77.0151,"mProvider":"fused","mSpeed":0.0,"mSpeedAccuracyMetersPerSecond":0.0,"mTime":1575977406000,"mVerticalAccuracyMeters":0.0},
"newSplit":false,"pointStatus":2,"time":{
"year":2019,"month":11,"dayOfMonth":10,"hourOfDay":6,"minute":30,"second":6}
}
请注意,所有与位置相关的数据都在此JSON字符串中。当我在API 28仿真器中运行相同的代码时,我得到以下JSON输出:
{"gpsData":[
{"elevation":40.8,"location":{
"mElapsedRealtimeNanos":9517448972700,"mProvider":"fused"},
"newSplit":false,"pointStatus":2,"time":{
"year":2019,"month":11,"dayOfMonth":10,"hourOfDay":6,"minute":32,"second":2}
}
请注意,位置数据均不在JSON字符串中。在这两种情况下,我都逐步执行了代码,并在执行gson.toJson(dataFinal)之前确认dataFinal对象中有数据。
[调试时,我在logcat中没有看到与GSON或JSON相关的条目。知道这是什么原因以及如何解决吗?
有关信息,这是我的课程:
public class GPSPointDataFinal {
private GregorianCalendar startTime;
private ArrayList<GPSPointData> gpsData;
private CollectGPSDataServiceStatus status;
private ArrayList<Integer> splitList;
public GregorianCalendar getStartTime() {
return startTime;
}
public void setStartTime(GregorianCalendar startTime) {
this.startTime = startTime;
}
public ArrayList<GPSPointData> getGpsData() {
return gpsData;
}
public void setGpsData(ArrayList<GPSPointData> gpsData) {
this.gpsData = gpsData;
}
public CollectGPSDataServiceStatus getStatus() {
return status;
}
public void setStatus(CollectGPSDataServiceStatus status) {
this.status = status;
}
public ArrayList<Integer> getSplitList() {
return splitList;
}
public void setSplitList(ArrayList<Integer> splitList) {
this.splitList = splitList;
}
}
公共类GPSPointData {
private Location location;
private double elevation;
private GregorianCalendar time;
private int pointStatus;
private boolean newSplit;
public GPSPointData(Location location, GregorianCalendar time, int status) {
this.location = location;
this.elevation = elevation;
this.time = time;
this.pointStatus = status;
}
public GPSPointData() {
}
public Location getLocation() {
return location;
}
public double getElevation() {
return elevation;
}
public GregorianCalendar getTime() {
return time;
}
public void setLocation(Location location) {
this.location = location;
}
public void setElevation(double elevation) {
this.elevation = elevation;
}
public void setTime(GregorianCalendar time) {
this.time = time;
}
public int getPointStatus() {
return pointStatus;
}
public void setPointStatus(int status) {
this.pointStatus = status;
}
public boolean isNewSplit() {
return newSplit;
}
public void setNewSplit(boolean newSplit) {
this.newSplit = newSplit;
}
}