Java 从android调用Soap Web服务
我正在使用下面的过程 1) 为SOAP请求创建字符串模板,并在运行时在此模板中替换用户提供的值以创建有效的请求。 2) 将此字符串包装在StringEntity中,并将其内容类型设置为text/xml 3) 在SOAP请求中设置此实体 在httppost的帮助下,我发布了这个请求 我正在使用w3schools.com的演示Web服务 网址---> 我试过的是Java 从android调用Soap Web服务,java,android,web-services,soap,Java,Android,Web Services,Soap,我正在使用下面的过程 1) 为SOAP请求创建字符串模板,并在运行时在此模板中替换用户提供的值以创建有效的请求。 2) 将此字符串包装在StringEntity中,并将其内容类型设置为text/xml 3) 在SOAP请求中设置此实体 在httppost的帮助下,我发布了这个请求 我正在使用w3schools.com的演示Web服务 网址---> 我试过的是 HttpPost httppost = new HttpPost("http://www.w3schools.com/webservic
HttpPost httppost = new HttpPost("http://www.w3schools.com/webservices/tempconvert.asmx");
StringEntity se;
try {
SOAPRequestXML="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/><soapenv:Body><tem:CelsiusToFahrenheit><!--Optional:--><tem:Celsius>30</tem:Celsius></tem:CelsiusToFahrenheit></soapenv:Body></soapenv:Envelope>";
Log.d("request is ", SOAPRequestXML+"!!!");
se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httppost.setEntity(se);
HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse =
(BasicHttpResponse) httpclient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
t.setText(EntityUtils.toString(resEntity));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpPost-HttpPost=新的HttpPost(“http://www.w3schools.com/webservices/tempconvert.asmx");
Stringse;
试一试{
SOAPRequestXML=“30”;
d(“请求为”,SOAPRequestXML+“!!!”);
se=新的StringEntity(SOAPRequestXML,HTTP.UTF_8);
setContentType(“text/xml”);
setHeader(“内容类型”,“应用程序/soap+xml;字符集=UTF-8”);
httppost.setEntity(se);
HttpClient HttpClient=新的DefaultHttpClient();
基本httpResponse httpResponse=
(BasicHttpResponse)httpclient.execute(httppost);
HttpEntity当前状态=httpResponse.getEntity();
t、 setText(EntityUtils.toString(resEntity));
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
我能够在soapui中得到响应,因此代码肯定是错误的,因为在emulator中我得到了输出 “由于媒体类型不受支持,服务器无法为请求提供服务。” 我是在HttpPost的构造函数中传递了正确的参数,还是发出了正确的xml请求。我做了很多尝试,但都没有找到答案
谢谢您是否尝试过使用适用于Android的ksoap2库 你可以在这里找到它,试一试:
希望这有帮助 我有一种预感,仿真器android版本和手机版本是不同的 但我没有什么建议。使用以下内容:
httppost.setHeader("Accept-Charset","utf-8");
httppost.setHeader("Accept","text/xml,application/text+xml,application/soap+xml");
同样,将内容类型设置为上述所有内容。我在c-sharpcorner.com上写了一篇关于如何设置的文章 很多人从那篇文章中得到了帮助。您也可以下载并运行它。我将帮助您了解如何将SOAP用于web服务 编辑 看看下面的链接。它使用ksoap进行复杂的数据处理
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
而不是
httppost.setHeader("Content-Type", "text/xml;charset=UTF-8");
正如您在URL中看到的请求->,他们正在使用
Content-Type: text/xml; charset=utf-8
并且您没有传递相同的内容类型。所以,它给了你一个错误
由于媒体类型不受支持,服务器无法为请求提供服务。
因此,只需更改标题,您就可以得到所需的响应。这样,html表单就会显示123摄氏度。没有肥皂或信封,只是工作:)
我可以使用ksoap2获得响应,但我不想这样,我只想要一个响应,然后我就可以解析它。我使用了你的方法,即ksoap2,效果很好(好文章)从这个w3schools web服务中获取结果。但我不希望这样,因为我将使用的服务将包含非常复杂的输入和输出xml。我想知道ksoap2能否在这方面帮助我。请确认。为什么不。它将帮助您绑定复杂数据并通过internet发送。您可以将其用于复杂的服务。嘿,我将使用java而不是.net制作的web服务。这样好吗?只有web服务是重要的,它是在哪个平台上开发的并不重要。是的,java web服务比PHP和.net稍微复杂一点是正确的。但是用java开发它很好。继续。输入xml正确吗,因为我使用soapui生成xml,并且输入xml与在soapui中生成的xml相同。这是正确的方法吗?
try {
HttpPost httppost = new HttpPost("http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit");
StringEntity se = new StringEntity("Celsius=123");
httppost.setEntity(se);
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse httpResponse = new DefaultHttpClient().execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
return EntityUtils.toString(resEntity);
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}