Service called from Android in C # not working

What I'm trying to do is send my Android image to my web service, which is C #. On the Android side, I get no errors and no warnings, but I have nothing to show on the service, but in fact it does not receive the image at all.

I am still particularly new to services this way, so any help would be greatly appreciated!

My Android AsyncTask Looks Like This:

@Override protected String doInBackground(File... file) { String imageDescriptionTemp = "Photo Temp Description."; String PostRequestUri = "https://demo.relocationmw.com/ws_docmgmt/Service1.svc"; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(PostRequestUri); FileBody bin1 = new FileBody(file[0]); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("Image", bin1); post.setEntity(entity); HttpResponse response; try { response = client.execute(post); resEntity = response.getEntity(); final String response_string = EntityUtils.toString(resEntity); if(resEntity != null){ Log.i("RESPONSE", response_string); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } 

The fact that there simply calls the service file on the server sends an image and soon the description and that’s all.

here is my c # utility code.

 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Xml; // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. public class Service1 : IService1 { #region vars XmlDocument doc = new XmlDocument();// Create the XML Declaration, and append it to XML document XmlElement root; #endregion public Stream GetImage(string path) { FileStream stream = File.OpenRead(@System.Web.Hosting.HostingEnvironment.MapPath("~/attachments/test/") + "SrvTest.jpg"); WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; return stream as Stream; } public XmlDocument UpImage(string path, Stream filecontents) { XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null); doc.PreserveWhitespace = true; doc.AppendChild(dec);// Create the root element root = doc.CreateElement("UpImage"); doc.AppendChild(root); XmlElement xml_item; xml_item = doc.CreateElement("Response"); Image img = System.Drawing.Image.FromStream(filecontents); img.Save(System.Web.Hosting.HostingEnvironment.MapPath("~/attachments/test/") + "SrvTest.jpg", ImageFormat.Jpeg); XmlElement item; item = doc.CreateElement("Success"); item.InnerText = "Success"; xml_item.AppendChild(item); return doc; } } 

This should take my file and return the XML with SUCCESS for me to register. Again, any help is much appreciated!

+4
source share
2 answers

Imagine that your application is a common html form loaded in IE, and submits exactly what this form submits. This way you can test your server component using a simple test form.

To get started, write a regular html form that will send the image to your service. Use an input element with a type attribute set to "file" and make sure that it gives the name of the element, not just the identifier ...

Once you can confirm that the html form message has been sent successfully, you will realize that the problem is related to your Android client code.

If the problem is in Android code, check the contents of the body. See that it is not null, etc., and then check that if the Content-Type and Content-Disposition headers are configured correctly.

If all of the above tests pass, check the boundaries of your posts. I was almost derailed by this problem twice a couple of years ago. (The number of invisible characters.)

Here is a good example for viewing: Uploading image to server in Multipart along with JSON data in Android

Although it has the other end (Java), the http rules are the same.

-Brandon

+1
source

Assuming the service is running, your problem is probably because WCF is adding the MIME header to the beginning of the stream.

This means that the received number of bytes seems longer than the sent number of bytes.

I found a good example of what to do here: http://www.ideatoappster.com/android-to-wcf-streaming-multi-part-binary-images/ Under the heading: WTF ?! My binary data contains more bytes when I read Stream in WCF than when I send it from Android

+1
source

All Articles