英文:
Gson().tojson() returning {} braces
问题
请有人帮助我找出问题所在...我想将 BluetoothDevice 对象存储到 SharedPrefrences 中,这样下次打开应用程序时就不需要再次选择配对设备。为此,我必须将这个 BluetoothDevice 对象转换为 String 变量,然后将其存储到 SharedPrefrence 中,我使用了以下代码:
Gson gson = new Gson();
String json = gson.toJson(selectedDevice, BluetoothDevice.class);
其中 selectedDevice 是 BluetoothDevice 类的一个对象。但是在运行代码后,我在 json 变量中得到了 {} 大括号。有人知道为什么会出现这种情况吗?
英文:
Please somebody help me to figure out the problem..I want to store BluetoothDevice object in to SharedPrefrences so that next time when I open the app not need to select the paring device again.For this I have to convert this BluetoothDevice object in to String variable and then store it in to SharedPrefrence for that I have used
Gson gson = new Gson();
String json = gson.toJson(selectedDevice,BluetoothDevice.class) ;
Where selectedDevice is an object of BluetoothDevice class. But after running the code I got the {} braces in json variable. Can anyone have any idea why this is happening.
答案1
得分: 1
几乎 android.bluetooth.BluetoothDevice
中的所有属性都是 static final
的。因此,这些属性不依赖于实例。
有一个 address
属性可用,取决于实例变量。您可以使用 getAddress()
将此 address
属性保存到共享首选项中。
如果蓝牙已启用,还可以使用 getName()
获取设备名称。
因此,我建议您将类似以下内容保存在共享首选项中:
class BluetoothDeviceInformation {
String address;
String name;
@RequiresPermission(Manifest.permission.BLUETOOTH)
public BluetoothDeviceInformation (
BluetoothDevice device
) {
this.address = device.getAddress();
this.name = device.getName();
}
}
您可以使用以下方式创建此实例:
BluetoothDeviceInformation deviceInformation =
new BluetoothDeviceInformation(selectedBluetoothDevice);
还有其他方法,如 getType()
、getAlias()
等。如果您需要它们的信息,只需将它们添加到您的 BluetoothDeviceInformation
类中。
英文:
Almost all of the properties in android.bluetooth.BluetoothDevice
are static final
. So, these properties do not depend on an instance.
One address
property is available and depends on the instance variable. You can just save this address
property to your shared-preferences using getAddress()
.
If bluetooth is enabled, you can, also, get device name with getName()
.
So, I suggest you save something like this in your shared-preferences;
class BluetoothDeviceInformation {
String address;
String name;
@RequiresPermission(Manifest.permission.BLUETOOTH)
public BluetoothDeviceInformation (
BluetoothDevice device
) {
this.address = device.getAddress();
this.name = device.getName();
}
}
You can create this instance using:
BluetoothDeviceInformation deviceInformation =
new BluetoothDeviceInformation(selectedBluetoothDevice);
There are other methods like getType()
, getAlias()
etc. If you need their information, just add them to your BluetoothDeviceInformation
class.
答案2
得分: 0
假设您指的是 https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java,其中包含静态字段,但是,
Java序列化仅对对象的非静态和非瞬态字段进行序列化
但是如果您真的需要,您可以尝试相同的方法:
BluetoothDevice selectedDevice = new BluetoothDevice();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT);
Gson gson = gsonBuilder.create();
String json = gson.toJson(selectedDevice, BluetoothDevice.class);
英文:
Assuming you are referring https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java, it contains static fields but,
Java Serialization only serialize object's non-static and non-transient fields
But if you really need it same way you can try:
BluetoothDevice selectedDevice = new BluetoothDevice();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT);
Gson gson = gsonBuilder.create();
String json = gson.toJson(selectedDevice, BluetoothDevice.class);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论