【发布时间】:2012-04-17 15:25:11
【问题描述】:
我在 android 中有一个 ArrayList,我试图将其转换为 Base64 字符串,然后通过 JSON 将其传递给 wcf 服务,然后 wcf 服务将字符串插入数据库。然后我需要能够从数据库中读取它并在 .NET 应用程序中对其进行反序列化。
这就是我在 android 中序列化 ArrayList 的方式:
private String convertByteArrayToSave(byte[] b){
if(b != null){
return Base64.encodeToString(b,0,b.length,Base64.DEFAULT);
}else{
return "";
}
}
private String convertListToStringToSave(ArrayList<myClasses.Shape> myList){
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream out;
try {
out = new ObjectOutputStream (b);
out.writeObject(myList);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertByteArrayToSave(b.toByteArray());
}
然后在 WCF 服务中,我尝试获取 Base64 字符串并将其反序列化为 List(Of Shape) 并将其序列化以将其插入数据库。
Private Function ConvertAndroidByteToDotNet(ByVal s As String) As Byte()
Dim result As Byte() = Nothing
Dim b As Byte() = CType(System.Convert.FromBase64String(s), Byte())
Dim msTest As New MemoryStream(b)
msTest.Seek(0, SeekOrigin.Begin)
msTest.Position = 0
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
formatter.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
formatter.Binder = New BindChanger()
Dim shapelist As List(Of ColbyDataTypes.Shape) = DirectCast(formatter.Deserialize(msTest), List(Of ColbyDataTypes.Shape))
msTest.Close()
Dim bin As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
bin.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
bin.Serialize(msTest, shapelist)
result = msTest.ToArray()
Return result
End Function
最终目标是能够在 .NET 应用程序或 android 应用程序中创建这些形状的列表,并且能够读取 .NET 应用程序和 android 应用程序中的列表,无论它是从哪里创建的.当您从 .NET 创建 Shape 并使用 WCF 将其读入 android 时,我能够做到这一点,但在 android 中创建它是我卡住的地方。非常感谢任何帮助,谢谢。
【问题讨论】:
标签: c# java android vb.net wcf