简体   繁体   English

将对象从Android客户端应用程序发送到C#Server App

[英]Sending object from Android Client App to C# Server App

Java Code: Java代码:

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: C#代码:

[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. 当我从Android客户端应用程序(Java编码)发送一个对象,我收到C#Server App中的对象,但有一个例外。 "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"; “输入流不是有效的二进制格式。起始内容(以字节为单位)为:00-05-73-72-00-1D-63-6F-6D-2E等”; Please suggest any simple solution. 请提出任何简单的解决方案 My project isn't that much complex. 我的项目并不复杂。 I just need to send an EMessage object. 我只需要发送一个EMessage对象。

Serialization formats are specific to the platforms, and Java and .NET serialization aren't compatible with each other. 序列化格式特定于平台,Java和.NET序列化彼此不兼容。 Use JSON instead (and it's easier to debug as well). 改为使用JSON(并且它也更容易调试)。

Why not use SOAP, here's an article on exactly what you're doing (android to .net) 为什么不使用SOAP,这里有一篇关于你正在做什么的文章(android到.net)

http://www.codeproject.com/Articles/29305/Consuming-NET-Web-Services-via-the-kSOAP-library 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. 我建议您出于上述原因(Java序列化与C#序列化不同)删除Serialization化,并在普通字节数组中在Java和C#应用程序之间传输数据。

You can convert your Bitmap image to a byte array like so ( taken from this post on SO ): 您可以将Bitmap图像转换为类似的字节数组( 取自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. 当然,如果情况需要,您可以更改CompressFormat After that, you could convert your type string to a byte array too, and add a null-terminator to the end of it. 之后,您也可以将type字符串转换为字节数组,并在其末尾添加一个null终止符。

Once you're there, you can send your type string first, and add the byte array of the bitmap after it. 一旦你在那里,你可以先发送你的type字符串,然后在它之后添加位图的字节数组。 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. 在C#端,您可以读取传入的数据,直到到达0终止符,此时您将知道您已读取EMessage对象的字符串部分,然后读取您发送的其余字节并将它们解析为Bitmap对象。

That way you'll be sure that between your Java and C# implementations, you won't run into any compatibility issues. 这样你就可以确定在Java和C#实现之间,你不会遇到任何兼容性问题。 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. 它可能需要更多的代码和更多的理解,但它比两种语言之间的序列化更可靠。

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

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