简体   繁体   English

在Windows中序列化对象列表并在WCF中反序列化

[英]Serialize list of objects in android and deserialize in WCF

I have an ArrayList in android that I'm trying to convert to a Base64 String, then passing that to a wcf service though JSON and the wcf service inserts the string into a database. 我在android中有一个ArrayList,我试图转换为Base64字符串,然后通过JSON将其传递给wcf服务,并且wcf服务将字符串插入数据库。 Then I need to be able to read that from the database and deserialize it in a .NET application. 然后我需要能够从数据库中读取它并在.NET应用程序中反序列化它。

This is how I am serializing the ArrayList in android: 这就是我在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());
}

Then in the WCF service I'm trying to take the Base64 string and deserialize it to the List(Of Shape) and serialize that to insert it into the database. 然后在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

The ultimate goal is to be able to be able to create a list of these shapes in the .NET app or android app and be able to read the list in the .NET app and android app no matter where it was created from. 最终目标是能够在.NET应用程序或Android应用程序中创建这些形状的列表,并能够读取.NET应用程序和Android应用程序中的列表,无论它是从哪里创建的。 I'm able to it when you create the Shape from .NET and read it into android using WCF but creating it in android is where I'm stuck. 当你从.NET创建Shape并使用WCF将其读入android时,我能够实现它,但是在android中创建它是我被困住的地方。 I really appreciate any help, thanks. 非常感谢任何帮助,谢谢。

I would choose JSON data format, and GSON to parse the objects in android side. 我会选择JSON数据格式,而GSON则解析android方面的对象。 It's very simple and lightweight to use. 它使用起来非常简单轻便。 Check this consuming web service from android using JSON or this JSON parse , hope this help. 使用JSON或此JSON解析 从android检查这个消费Web服务 ,希望这个帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM