我正在使用下面的过程,
1) 为 SOAP 请求创建一个字符串模板,并在运行时在此模板中替换用户提供的值以创建有效请求。 2) 将此字符串包装在 StringEntity 中并将其内容类型设置为 text/xml 3) 在 SOAP 请求中设置该实体。
在 httppost 的帮助下,我发布了请求,
我正在使用来自 w3schools.com 的演示网络服务
网址--->
http://www.w3schools.com/webservices/tempconvert.asmx
我试过的是,
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();
}
我能够在 soapui 中得到响应,所以代码肯定是错误的,因为在模拟器中我得到了输出,
“服务器无法为请求提供服务,因为不支持媒体类型”。
我是否在 HttpPost 的构造函数中传递了正确的参数,或者我是否发出了正确的 xml 请求。我尝试了很多但无法弄清楚。
谢谢
最佳答案
您的代码唯一的问题是您将 Header 设置为,
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
而不是,
httppost.setHeader("Content-Type", "text/xml;charset=UTF-8");
如您所见,URL 中的请求是 -> w3schoools , 他们正在使用,
Content-Type: text/xml; charset=utf-8
并且您没有传递相同的内容类型。所以,它给了你错误,
服务器无法为请求提供服务,因为媒体类型不受支持。
因此,只需更改标题即可获得所需的响应。
关于java - 从 Android 调用 Soap Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16374354/