gpt4 book ai didi

java - DELPHI中如何将JObject转换为TJavaArray

转载 作者:太空狗 更新时间:2023-10-29 14:54:46 29 4
gpt4 key购买 nike

DELPHI中如何将JObject转为TJavaArray?

这个方法我试过了,没有结果

var
jvObject: JObject;
jvArray: TJavaArray<byte>;
begin
jvArray:= TJavaArray<byte>(jvObject);

因为 jvArray 的结果是nil

有人知道如何解决此转换吗?

最佳答案

我认为,在 Java 术语中,您要求的是序列化对象。

这是一个可以解决问题的单元:

unit SerialiseU;

interface

uses
Androidapi.JNI.JavaTypes,
Androidapi.JNIBridge;

//References:
// http://docs.oracle.com/javase/6/docs/platform/serialization/spec/serialTOC.html
// http://stackoverflow.com/questions/5837698#5837739

type
TSerialiser = class
public
class function Serialise(const Obj: JObject): TJavaArray<Byte>;
class function Deserialise(Bytes: TJavaArray<Byte>): JObject;
end;

implementation

// Delphi XE5 and later (all Android-supporting versions) offer an
// import for java.io.ByteArrayOutputStream and java.io.ByteArrayInputStream
// in Androidapi.JNI.JavaTypes.pas.
// However neither java.io.ObjectInputStream nor java.io.ObjectOutputStream
// are imported in any version up to the current (at time of writing) version,
// Delphi 10.1 Berlin, so we import the bits we need here.

type
// ===== Forward declarations =====

JObjectInputStream = interface;//java.io.ObjectInputStream
JObjectOutputStream = interface;//java.io.ObjectOutputStream

// ===== Interface declarations =====

JObjectInputStreamClass = interface(JInputStreamClass)
['{443A1BEF-E21F-4012-A28B-4D7735136BD3}']
{class} function init(input: JInputStream): JObjectInputStream; cdecl;//Deprecated
end;

[JavaSignature('java/io/ObjectInputStream')]
JObjectInputStream = interface(JInputStream)
['{C1360ABB-AF58-4607-B43E-C1E1652E8FC2}']
function readObject: JObject; cdecl;//Deprecated
end;
TJObjectInputStream = class(TJavaGenericImport<JObjectInputStreamClass, JObjectInputStream>) end;

JObjectOutputStreamClass = interface(JOutputStreamClass)
['{D43CF30C-1E94-4D2E-A473-91EE54E41F07}']
{class} function init(output: JOutputStream): JObjectOutputStream; cdecl;//Deprecated
end;

[JavaSignature('java/io/ObjectOutputStream')]
JObjectOutputStream = interface(JOutputStream)
['{F4E441F8-B3D0-4463-A052-880F6644FB42}']
procedure writeObject(&object: JObject); cdecl;
end;
TJObjectOutputStream = class(TJavaGenericImport<JObjectOutputStreamClass, JObjectOutputStream>) end;

{ TSerialiser }

class function TSerialiser.Serialise(const Obj: JObject): TJavaArray<Byte>;
var
b: JByteArrayOutputStream;
o: JObjectOutputStream;
begin
b := TJByteArrayOutputStream.Create;
o := TJObjectOutputStream.JavaClass.init(b);
o.writeObject(Obj);
Result := b.toByteArray;
end;

class function TSerialiser.Deserialise(Bytes: TJavaArray<Byte>): JObject;
var
b: JByteArrayInputStream;
o: JObjectInputStream;
begin
b := TJByteArrayInputStream.JavaClass.Init(Bytes);
o := TJObjectInputStream.JavaClass.init(b);
Result := o.readObject;
end;

end.

可以这样使用:

uses
SerialiseU,
Androidapi.Helpers,
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes;
...
var
S, S2: String;
JS, JS2: JString;
O: JObject;
Bytes: TJavaArray<Byte>;
...
// Get some Java object. In this case it is a Java java.lang.String object
// http://d.android.com/reference/java/lang/String.html
S := Edit1.Text;
JS := StringToJString(S);
// Serialise the string into a byte array
Bytes := TSerialiser.Serialise(JS);
// Now de-serialise the byte array back to an object
O := TSerialiser.Deserialise(Bytes);
// Prove that the correct object type has been de-serialised
ShowMessageFmt('Deserialised a %s object',
[JStringToString(O.getClass.getCanonicalName)]);
// Cast the generic object back to a Java string
JS2 := TJString.Wrap(O);
// Get a Delphi string out, just to dot the 'i's and cross the 't's
S2 := JStringToString(JS2);
// Put the extracted string back on the UI so we can prove it all worked
Edit1.Text := S2;
end;

关于java - DELPHI中如何将JObject转换为TJavaArray<byte>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32679085/

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