我想做的是将我的图像从 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 供我记录。再次,非常感谢任何帮助!