从 Android 获取 & Post 图片到 WCF

Get & Post image from Android to WCF

我想将图像连同文本从我的 android 客户端发送到本地主机上的 wcf Web 服务。我已经成功地发送和接收了文本,因为发送文本对我来说非常困难,但现在我必须连同文本一起发送图像。我不想像使用流等那样改变我的方法。我想将图像作为 json 传递并在我的网络服务中接收它并将其存储在 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 代码。我尝试了不同的代码,但 none 有效。上面的两个代码适用于 C# 客户端,但我需要它在 java 即 android.

JSONObject json = new 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);

我的网络服务class问题

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 这是我的网络服务的界面

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() 而不是 byte,但这也不起作用。

我不知道你从android那边得到了什么错误,但你也可以尝试用HttpUrlConnection发送数据检查this answer

如果您想使用 HttpPost,请查看此答案:

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);