gpt4 book ai didi

java - 如何使用gson序列化android中具有自定义对象属性的类?

转载 作者:行者123 更新时间:2023-11-30 01:34:10 27 4
gpt4 key购买 nike

我想使用 Gson 库序列化和存储一个名为“Battery”的自定义类的对象。在 Battery 类中,我定义了另一个自定义对象的数组,即“Cannon”。这是我的两个类(class):

大炮.java:

import android.widget.ImageView;

public class Cannon {
private String id; // used for object storage
private int number; // Cannon number in battery
private ImageView icon;

private long utmEast, utmNorth; // Used to define cannon global location
private boolean isBase; // Defines if the cannon is base

private double flyingTime;
private int angle, side;

public Cannon() {

}

public Cannon(String id, int number, ImageView icon, String connectionNumber){
this.id = id;
this.number = number;
this.icon = icon;
this.connectionNumber = connectionNumber;
}

public String getId() {
return this.id;
}

public int getNumber() {
return this.number;
}

public ImageView getIcon() {
return this.icon;
}

public long getUtmNorth() {
return utmNorth;
}

public void setUtmNorth(long utmNorth) {
this.utmNorth = utmNorth;
}

public long getUtmEast() {
return utmEast;
}

public void setUtmEast(long utmEast) {
this.utmEast = utmEast;
}

public void setAsBase(boolean isBase) {
this.isBase = isBase;
}

public boolean isBaseCannon() {
return isBase;
}
}

电池.java:

public class Battery {

private String id; // used for object storage

private final Cannon cannons[] = new Cannon[6];

public Battery(String id){
this.id = id;
}

public String getId() {
return this.id;
}

public Cannon[] getCannons() {
return cannons;
}

public void setCannon(Cannon cannon, int position) {
this.getCannons()[position] = cannon;
}

public Cannon getCannon(String cannonId) {
for (Cannon cn: this.getCannons()) {
if (cn.getId() == cannonId) {
return cn;
}
}
return null;
}

public Cannon getCannon(int position) {
return getCannons()[position];
}
}

这是我的 Preferences 类,它使用共享首选项来存储应用程序特定字段,包括使用 Gson 库的电池对象。

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;

import net.adeveloper.firecontrol.data.Battery;

/** stores the user object in SharedPreferences */
public class Preferences {
/** This application's preferences label */
private static final String PREFS_NAME = "net.adeveloper.firecontrol.preferences";
/** This application's preferences */
private static SharedPreferences settings;
/** This application's settings editor*/
private static SharedPreferences.Editor editor;
/** Constructor takes an android.content.Context argument*/
private final Gson gson = new Gson();

/**************************************************/

private final Battery battery = new Battery(Reference.BATTERY_ID);

/**************************************************/

public Preferences(Context ctx){
if(settings == null){
settings = ctx.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE );
}
/*
* Get a SharedPreferences editor instance.
* SharedPreferences ensures that updates are atomic
* and non-concurrent
*/
editor = settings.edit();

// Initialize battery
String battery_json = gson.toJson(battery);
// store in SharedPreferences
String id = battery.getId(); // get storage key
editor.putString(id, battery_json);
editor.commit();
}

public Battery getBattery() {
String battery_json = settings.getString(Reference.BATTERY_ID, "");
Battery battery = gson.fromJson(battery_json, Battery.class);
return battery;
}

//...

}

我在 MainActivity 中使用首选项对象来存储应用程序字段:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//...

/********************************************************/

prefs = new Preferences(this.getApplicationContext());

/********************************************************/

然而,当它启动时应用程序崩溃并输出 Gson stackoverflowError:

02-10 16:39:03.110 13187-13187/net.adeveloper.firecontrol E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.StackOverflowError
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:355)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:117)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
at com.google.gson.Gson.getAdapter(Gson.java:356)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
at com.google.gson.Gson.getAdapter(Gson.java:356)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.cr

更新:

我在 setContentView 之后将这段代码添加到我的 MainActivity 中,因为我使用的是 Android 版本为 4.2.1 的设备:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

//...

/********************************************************/

prefs = new Preferences(this.getApplicationContext());

/********************************************************/

现在 stackoverflow 异常已经消失,但现在出现了另一个问题,即 toJson() 和 fromJson() 方法返回的我的 Json 字符串为空,当我使用 Preference 对象通过电池类设置我的 cannon 对象时实际上无法访问再次返回 null:

Cannon cannon = new Cannon(id, number, icon, phone);
prefs.getBattery().setCannon(cannon, position);
if (prefs.getBattery().getCannons() == null) {
Log.d("Cannon", "NULL");
} else {
for (Cannon cn: prefs.getBattery().getCannons()) {
Log.d("Cannon", cn + "");
}
}

输出:

Cannon: null
Cannon: null
Cannon: null
Cannon: null
Cannon: null
Cannon: null

最佳答案

我可能没有得到你想要达到的目标??但你可以
1. 对象转json
2.保存在SharedPreference然后
3. 再次可以从首选项中获取 json
4. 并转换为java对象

例子

class Test
{
int id = 1;
int name = "testing";
}

class Main{
Test t = new Test;
String s = ConvertToJson(t);

/* save the string in SharedPreferecne and you can get any
time string and convert again for eg.*/

Test t2 = ConvertToTest(TestJsonSring);

public Test ConvertToTest(String json) {
return new Gson().fromJson(json, Test.class);
}

public String ConvertToJson(Object object) {
Gson gson = new Gson();
return gson.toJson(object);
}
}

关于java - 如何使用gson序列化android中具有自定义对象属性的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35323353/

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