【发布时间】:2019-12-10 11:53:47
【问题描述】:
以下代码 sn-p 在运行 API 26 的模拟器实例与运行 API 28 的模拟器实例之间的工作方式不同时遇到问题。这是我正在运行以将 GPS 点数据对象转换为 JSON 的代码 sn-p .
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;
}
}
【问题讨论】:
-
您的目标是哪个 Android SDK 版本?
-
可能您需要启用一些在
API 26中默认启用的访问。看看:Android 9 (API level 28) introduces a number of changes to the Android system.和Android background location limits -
我的 compileSdkVersion 是 28,我的 targetSdkVersion 是 28。
-
我认为问题可能在于,从 API 28 开始,Location 的某些成员现在被注释为 UnsupportedAppUsage,如果您的目标是 28 岁以上,它们就会被列入黑名单。奇怪的是,它们是生成的 JSON 中出现的仅有的两个字段——我本来预计会出现相反的情况(除了 JSON 中的所有字段)或应用程序崩溃。无论如何,我认为一个解决方案(和良好的做法)是只从位置序列化您需要的内容,而不是尝试序列化整个实例。
-
Clownba0t,我最终将我需要的特定字段从 Location 对象中提取到我的 GPSPointData 对象中,并且只对其进行了序列化。这解决了问题......虽然我不明白为什么部分 Location 对象成功序列化。哦,好吧...谢谢您的帮助!