0

i wanna send image along with text from my android client to wcf web service on localhost. i have successfully sent text and received as well and as it was quite difficult for me just to send text but now i have to send image along with text as well. i'd prefer not to change my method like using stream etc. i want to pass image as json and receive it in my web service and store it in sql database image column or even storing on disc would do. So please tell me what changes i need to make in code below to do it. Thanks in advance...!

here's my AddIssue method of web service.

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

and here's my java code. i have tried different codes but none worked. the above two codes work with a C# client but i need it in java i.e 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);

issue class of my web service

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 here's interface of my web service

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

i have tried directly using json.toString() instead of byte but that didn't work either.

Lelouch
  • 13
  • 6

1 Answers1

0

I don't know what error you get from android side but you can also try with HttpUrlConnection to send data check this answer.

Or look this answer if you want to use 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);

On C# get image string then change to 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);
Community
  • 1
  • 1
misman
  • 1,347
  • 3
  • 21
  • 39