从Android调用到c#的服务不工作
本文关键字:服务 工作 Android 调用 | 更新日期: 2023-09-27 18:08:58
我想做的是将我的图像从Android发送到我的web服务,这是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供我记录。再次,任何帮助是非常感谢!
假设你的应用程序是一个加载到IE中的普通html表单,并发送该表单应该发送的内容。这样你就可以用一个简单的测试表单来测试你的服务器组件了。
首先,编写一个普通的html表单,向您的服务提交一个图像。使用type属性设置为"file"的输入元素,并确保给元素一个名称,而不仅仅是一个id…
一旦你可以确认html表单发布成功,你就知道问题出在你的android客户端代码。
如果问题在你的android代码中,检查代码体的内容。确保它不是null等,然后检查它是否正确设置了Content-Type和Content-Disposition头。
如果以上所有测试都通过,请检查您的帖子的边界线。几年前,我差点被这个问题搞得脱轨两次。(不可见字符计数)
这里有一个很好的例子:在Multipart中上传图像到服务器,在Android中使用JSON数据
虽然它有不同的后端(Java), http规则是相同的。
布兰登
假设服务是可操作的,您的问题可能在于WCF在流的开头添加了一个MIME头。
这将意味着接收到的字节数似乎比发送的字节数长。
我在这里找到了一个很好的例子:http://www.ideatoappster.com/android-to-wcf-streaming-multi-part-binary-images/标题下:WTF?!当我在WCF中读取流时,我的二进制数据包含比从Android