java - Get & Post image from Android to WCF -


i wanna send image along text android client wcf web service on localhost. have sent text , received , quite difficult me send text have send image along text well. i'd prefer not change method using stream etc. want pass image json , receive in web service , store in sql database image column or storing on disc do. please tell me changes need make in code below it. in advance...!

here's 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 [order] record id             return -3;         }         dc.issues.insertonsubmit(currentissue);         // update our sql server [order] record, our new shipping details (send whatever         // app calling web service)          dc.submitchanges();          return 0;     // success !     }     catch (exception)     {         return -1;     } } 

and here's java code. have tried different codes none worked. above 2 codes work c# client need 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 bitmap object               byte[] b = baos.tobytearray();            string encodedimage = base64.encodetostring(b , base64.default);            json.put("image",encodedimage); 

issue class of 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 web service

namespace jsonwebservice {      // note: can use "rename" command on "refactor" menu change interface name "iservice1" in both code , 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 didn't work either.

i don't know error android side can try httpurlconnection send data check this answer.

or answer if want 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 bitmap object    byte[] b = baos.tobytearray();  string encodedimage = base64.encodetostring(b , base64.default); args.put("image",encodedimage);  //you can 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# image string change 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); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -