繁体   English   中英

从Android获取图像并将图像发布到WCF

[英]Get & Post image from Android to WCF

我想将图像以及文本从我的android客户端发送到localhost上的wcf Web服务。 我已经成功发送和接收了文本,因为仅发送文本对我来说是相当困难的,但现在我也必须同时发送图像和文本。 我不希望像使用流等那样更改我的方法。我想将图像作为json传递并将其接收到我的Web服务中,并将其存储在sql数据库图像列中,甚至存储在磁盘上也可以。 所以,请告诉我我需要在下面的代码中进行哪些更改。 提前致谢...!

这是我的Web服务的AddIssue方法。

    public int AddIssue(Issue issue)
{
   // byte[] bm=System.Convert.FromBase64String(issue.Image.ToString());
    //Binary bo = new Binary(bm);

    try
    {
        NorthwindDataContext dc = new NorthwindDataContext();
        Issue currentIssue = new Issue
        {
            Area = issue.Area,
            Description= issue.Description,
           // Image =bo

        };

        if (currentIssue == null)
        {
            // Couldn't find an [Order] record with this ID
            return -3;
        }
        dc.Issues.InsertOnSubmit(currentIssue);
        // Update our SQL Server [Order] record, with our new Shipping Details (send from whatever
        // app is calling this web service)

        dc.SubmitChanges();

        return 0;     // Success !
    }
    catch (Exception)
    {
        return -1;
    }
}

这是我的Java代码。 我尝试了不同的代码,但是没有用。 上面的两个代码与C#客户端一起工作,但我需要在Java即android中使用。

JSONObject json =新的JSONObject();

           json.put("Area",editTextPrice.getText().toString()); 
           json.put("Description",editTextDescription.getText().toString());
           img.buildDrawingCache();
           Bitmap bm=img.getDrawingCache();
           ByteArrayOutputStream baos = new ByteArrayOutputStream();  
           bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
           byte[] b = baos.toByteArray();
           String encodedImage = Base64.encodeToString(b , Base64.DEFAULT);
           json.put("Image",encodedImage);

我的网络服务的发行类别

namespace JSONWebService
{
    [DataContract]
    [Serializable]
    public class wsIssue
    {
        [DataMember]
        public int IssueId { get; set; }

        [DataMember]
        public string Area { get; set; }

        [DataMember]
        public string Description { get; set; }
        [DataMember]
        public string Image { get; set; }

    }

n这是我的Web服务的界面

namespace JSONWebService
{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "getAllIssues")]
        List<wsIssue> GetAllIssues();
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json, UriTemplate = "AddIssue")]
        int AddIssue(Issue issue);

我已经尝试过直接使用json.toString()而不是字节,但是那也不起作用。

我不知道您从android端遇到什么错误,但是您也可以尝试使用HttpUrlConnection发送数据来检查此答案

或者如果您想使用HttpPost 查看以下答案: https : //stackoverflow.com/questions/6360207/android-sending-a-byte-array-via-http-post

JSONObject args = new JSONObject();
args.put("Area", editTextPrice.getText().toString());
args.put("Description", editTextDescription.getText().toString())

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray();

String encodedImage = Base64.encodeToString(b , Base64.DEFAULT);
args.put("image",encodedImage);

//You can also use NameValuePair instead JSON
//like : nameValuePairs.add(new BasicNameValuePair("Area", editTextPrice.getText().toString());
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", arg.toString()));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

在C#上获取图像字符串,然后更改为bytearray

public class Message
{

 public string Area {get;set;}
 public string Description {get;set;}
 public string Image {get;set;}
}
Message message = new JavaScriptSerializer().Deserialize<Message>(result);

byte[] imageData = Convert.FromBase64String(message.image);
MemoryStream ms = new MemoryStream(imageData);
Image returnImage = Image.FromStream(ms);

暂无
暂无

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

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