简体   繁体   English

通过webservice从ms sql数据库发送字节数组到android

[英]Send byte array from ms sql database to android through webservice

I have a C# web service that gets data from a MS SQL database and sends it to an Android device as a XML string. 我有一个C#Web服务,它从MS SQL数据库获取数据并将其作为XML字符串发送到Android设备。

I then parse the XML in Android and store the data in an Android database 然后我在Android中解析XML并将数据存储在Android数据库中

This works fine for all string and integer data but I can't get a byte array (which is used to store an image) to work in same way 这适用于所有字符串和整数数据,但我不能得到一个字节数组(用于存储图像)以相同的方式工作

My C# code to add the byte array to XML looks like this: 将字节数组添加到XML的我的C#代码如下所示:

XmlElement ImageStored = (XmlElement)StockItem.AppendChild(doc.CreateElement("ImageStored"));

ImageStored.InnerText = Convert.ToBase64String(stockItem.ImageStored);

And in Android i have tried this: 在Android中我试过这个:

NodeList nImageStored = doc.getElementsByTagName("ImageStored");

for(int i = 0; i < nImageStored.getLength(); i++)
{
   byte[] pImageStored =  nImageStored.item(i).getFirstChild().getNodeValue().trim().getBytes();
   //save byte[] in database
}

This is not giving me any errors and does save something in the database, but when it comes to displaying the image nothing appears. 这不会给我任何错误,并且会在数据库中保存一些内容,但是当显示图像时,没有任何内容出现。

Any help on how to get this working would be great. 任何有关如何使这项工作的帮助都会很棒。 This is the first time I have tried working with C# and Android together so forgive me if I am being stupid. 这是我第一次尝试使用C#和Android,所以请原谅我,如果我是愚蠢的。

Thanks! 谢谢!

I'm not much of a Java guy but it looks like you should be using decode() instead of getBytes(). 我不是一个Java人,但看起来你应该使用decode()而不是getBytes()。 In other words you are just storing the Base64 encoded string in your database, instead of decoding it first. 换句话说,您只是将Base64编码的字符串存储在数据库中,而不是先将其解码。

public static byte[] decode (String str, int flags) public static byte [] decode(String str,int flags)

Since: API Level 8 Decode the Base64-encoded data in input and return the data in a new byte array. 从以下版本开始:API Level 8解码输入中的Base64编码数据,并以新的字节数组返回数据。 The padding '=' characters at the end are considered optional, but if any are present, there must be the correct number of them. 末尾的填充'='字符被认为是可选的,但如果存在,则必须有正确的数字。

http://developer.android.com/reference/android/util/Base64.html http://developer.android.com/reference/android/util/Base64.html

I have done almost the same where I used the thirdparty library ksoap2. 我使用第三方库ksoap2几乎一样。 Ok so here's an example, first I created a wrapper class for my object which needed the image: 好的,这是一个例子,首先我为我的对象创建了一个需要图像的包装类:

public class ImageClass {
    private byte[] AttractionImage;
    private String AttractionImageBase64;
        // TODO getter and setters
}

First you need to decode the base64 string you get from the .NET webservice, for that I also used the ksoad library: 首先,您需要解码从.NET Web服务获得的base64字符串,因为我还使用了ksoad库:

ImageClass imageClass = new ImageClass();
imageClass.setAttractionImage(Base64.decode("your base62 string"));

Let me know if you need more info :) 如果您需要更多信息,请告诉我:)

you can find ksoap here: http://code.google.com/p/ksoap2-android/ 你可以在这里找到ksoap: http//code.google.com/p/ksoap2-android/

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

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