【问题标题】:Service Called from Android to C# not working从 Android 调用到 C# 的服务不起作用
【发布时间】:2013-04-05 07:50:15
【问题描述】:

我想要做的是将我的图像从 Android 发送到我的 C# 网络服务。 在 Android 端,我没有收到任何错误和警告,但在服务上没有任何可显示的内容,它根本没有真正获取图像。

我对这种方式的服务仍然特别陌生,所以任何帮助都将不胜感激!

我的 Android AsyncTask 看起来像这样:

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

那里只是调用服务器上的服务文件,发送图像,很快就会有描述和一切。

这是我的 C# 服务代码。

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;

    }
}

这应该接受我的文件并返回一个带有 SUCCESS 的 XML 供我记录。 再次感谢任何帮助!

【问题讨论】:

  • 你们的服务有效吗?你用 SoapUI 测试过吗?如果不试试看!
  • 这是一项 REST 服务,如果可以,该工具是否适用于此 WSDL 是什么?根据我使用 .Net 的经验,我查看了这段代码,它应该可以工作。
  • 用肥皂试一试,我听说其他的 c# 不好用(我不是 100% 肯定)。我在 C# 服务中使用了 soap。

标签: c# android web-services post web


【解决方案1】:

假设您的应用程序是加载到 IE 中的常见 html 表单,并准确发送该表单将发送的内容。这样您就可以使用简单的测试表单来测试您的服务器组件。

首先,编写一个向您的服务提交图像的纯 html 表单。使用 type 属性设置为“file”的 input 元素,并确保为元素命名,而不仅仅是 id...

一旦您确认 html 表单发布成功,您就会知道问题出在您的 android 客户端代码中。

如果问题出在您的 android 代码中,请检查正文的内容。看到它不为空等,然后检查是否正确设置了 Content-Type 和 Content-Disposition 标头。

如果上述所有测试均通过,请检查帖子的边界线。几年前,我两次差点被这个问题脱轨。 (不可见字符数。)

这是一个很好的例子:Uploading image to server in Multipart along with JSON data in Android

虽然它有不同的后端(Java),但 http 规则是相同的。

-布兰登

【讨论】:

    【解决方案2】:

    假设服务是可操作的,您的问题可能在于 WCF 在流的开头添加了一个 MIME 标头。

    这意味着接收的字节数似乎比发送的字节数长。

    我在这里找到了一个很好的例子: http://www.ideatoappster.com/android-to-wcf-streaming-multi-part-binary-images/ 在标题下:WTF?!当我在 WCF 中读取 Stream 时,我的二进制数据包含的字节数比我从 Android 发送时包含的字节数多

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-17
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2015-02-23
      • 1970-01-01
      相关资源
      最近更新 更多