简体   繁体   中英

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. Then I need to be able to read that from the database and deserialize it in a .NET application.

This is how I am serializing the ArrayList in android:

    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.

    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. 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. I really appreciate any help, thanks.

I would choose JSON data format, and GSON to parse the objects in android side. It's very simple and lightweight to use. Check this consuming web service from android using JSON or this JSON parse , hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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