简体   繁体   English

Android将字节数组(图像)发布到WCF Restful服务

[英]Android post byte array(image) to WCF restful service

I have a WCF service that I am able to post strings to with no problem but now I'm trying to post a byte array as a string and this is where I am getting issues. 我有一个WCF服务,可以将字符串毫无问题地发布到其中,但是现在我正尝试将字节数组作为字符串发布,这就是我遇到问题的地方。 My goal is to take a picture, convert it to a byte(), send that to the WCF service and the WCF service will insert into my database. 我的目标是拍摄一张照片,将其转换为byte(),然后将其发送到WCF服务,WCF服务将插入到我的数据库中。 I'm not sure if I'm going at this the wrong way though, I can't find any good explainations on saving an image to WCF. 我不确定是否会采用这种错误的方式,但是我找不到将图像保存到WCF的任何好方法。

Here is the android code for the httpposting: 这是httpposting的android代码:

        HttpPost request = new HttpPost(SERVICE_URL + "/SaveAssmt");
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        JSONStringer assmt = new JSONStringer()
        .object()
            .key("data")
                .object()
                    .key("sFacID").value(res.get_sFacID())
                    .key("sResID").value(res.get_sResID())                   
                    .key("byteImage").value(new String(imagedata)) //imagedata is my byte() containing the image
                .endObject()
            .endObject();
        StringEntity entity = new StringEntity(assmt.toString());

        request.setEntity(entity);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);

Here are the WCF pieces: 以下是WCF片段:

<OperationContract()> _
<WebInvoke(Method:="POST", UriTemplate:="SaveAssmt", BodyStyle:=WebMessageBodyStyle.WrappedRequest, ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
Sub SaveAssmt(data As AssmtResponses)

and then my SaveAssmt method is just an insert into the database which works as long as i don't pass the imagedata. 然后我的SaveAssmt方法只是插入数据库,只要我不传递imagedata就可以。 As soon as i pass the imagedata, I get a 400: Bad Request error so I'm not sure if its because the byte size is too large or what the case might be. 传递图像数据后,我立即收到400:Bad Request错误,因此我不确定是否是因为字节大小太大或什么情况。 If anyone can point me in the right direction, I'd really appreciate any help. 如果有人能指出正确的方向,我将非常感谢您的帮助。 Thanks. 谢谢。

Encode with a standard format, like Base64 (common in web and mobile applications). 使用标准格式进行编码,例如Base64(在Web和移动应用程序中常见)。 You can then either a) reconsitute the image as binary on the server side or b) store the encoded string in a database and serve as needed. 然后,您可以a)在服务器端将图像重新配置为二进制图像,或者b)将编码后的字符串存储在数据库中并根据需要提供服务。 The answer depends on your particular application needs. 答案取决于您的特定应用程序需求。

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

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