我在 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创建形状并使用WCF将其读入android时,我可以做到这一点,但在android中创建它是我陷入困境的地方。我真的很感谢任何帮助,谢谢。
最佳答案
我会选择JSON数据格式,和GSON解析android端的对象。使用起来非常简单且轻便。检查这个consuming web service from android using JSON或者这个JSON parse ,希望这有帮助。
关于c# - 在android中序列化对象列表并在WCF中反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10194134/