【发布时间】: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