简体   繁体   中英

Sending object from Android Client App to C# Server App

Java Code:

public class EMessage implements Serializable
{

private Bitmap image;

private String type;

EMessage()

{}

}
...

EMessage eMessage=new EMessage();

outToServer = new DataOutputStream(clientSocket.getOutputStream());

objectOutputStream=new ObjectOutputStream(outToServer);

objectOutputStream.writeObject(eMessage);

C# Code:

[Serializable]

class EMessage

{

    private Bitmap image;

    private String type;

    EMessage()

    { }

}

client = server.AcceptTcpClient();

 Connected = client.Connected;

            ns = client.GetStream();
 IFormatter formatter = new 

 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

 EMessage recievedmsg = (EMessage)formatter.Deserialize(ns);

When I send an object from Android Client App (java coded) and I recieve the object in C# Server App but with an Exception. "The Input Stream is not a valid binary format. The Starting Content(in bytes) are: 00-05-73-72-00-1D-63-6F-6D-2E etc"; Please suggest any simple solution. My project isn't that much complex. I just need to send an EMessage object.

Serialization formats are specific to the platforms, and Java and .NET serialization aren't compatible with each other. Use JSON instead (and it's easier to debug as well).

Why not use SOAP, here's an article on exactly what you're doing (android to .net)

http://www.codeproject.com/Articles/29305/Consuming-NET-Web-Services-via-the-kSOAP-library

I suggest you drop the Serialization for the above mentioned reasons (Java serialization being different from C# serialization), and transfer your data between your Java and C# applications in plain byte arrays.

You can convert your Bitmap image to a byte array like so ( taken from this post on SO ):

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Of course you could change the CompressFormat if circumstances so require. After that, you could convert your type string to a byte array too, and add a null-terminator to the end of it.

Once you're there, you can send your type string first, and add the byte array of the bitmap after it. On the C# end, you could read the incoming data until you reach the 0 terminator, at which point you'll know you've read the string portion of your EMessage object, and then read the rest of the bytes you've sent over and parse them into a Bitmap object.

That way you'll be sure that between your Java and C# implementations, you won't run into any compatibility issues. It may require a bit more code and a little more understanding to do, but it's far more reliable than serializing between two languages.

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