我正在尝试从我的 Android 应用程序访问 SOAP 服务。
我正在使用 http://www.requestmaker.com/ 测试我的请求所以我最终提出了这个请求:
已发送请求 header :
POST /WebserviceHCX.asmx HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: hcxwords/installAcknowledge
Host: webservice.hcxwords.eu
Accept-Charset: utf-8
Accept: text/xml,application/text+xml,application/soap+xml
Content-Length: 382
请求数据:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<installAcknowledge xmlns="hcxwords">
<deviceID>test</deviceID>
<refCode2></refCode2>
</installAcknowledge>
</soap:Body>
</soap:Envelope>
因为这在 http://www.requestmaker.com/ 中运行得非常好(200 OK)我只是想在我的 Android 应用程序中做同样的事情。
final String SOAPRequestXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><installAcknowledge xmlns=\"hcxwords\"><deviceID>"+deviceID+"</deviceID><refCode2>"+refCode2+"</refCode2></installAcknowledge></soap:Body></soap:Envelope>";
String url = "http://webservice.hcxwords.eu/WebserviceHCX.asmx";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-type", "text/xml;charset=UTF-8");
httppost.addHeader("SOAPAction", "hcxwords/installAcknowledge");
httppost.addHeader("Host", "webservice.hcxwords.eu");
httppost.addHeader("Accept-Charset", "utf-8");
httppost.addHeader("Accept", "text/xml,application/text+xml,application/soap+xml");
StringEntity se;
try {
se = new StringEntity(SOAPRequestXML, HTTP.UTF_8);
se.setContentType("text/xml;charset=UTF-8");
se.setContentEncoding("utf-8");
httppost.setEntity(se);
// Execute HTTP Post Request
BasicHttpResponse httpResponse =
(BasicHttpResponse)httpclient.execute(httppost);
return httpResponse;
} catch (ClientProtocolException e) {
e.printStackTrace();
throw new IllegalStateException("ClientProtocolException ");
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException("IOException ");
}
这在我的模拟器(200 OK)中工作得很好,在我的 Nexus 4 上它是(404),在我的三星平板电脑上它抛出 IOException:
IOException: Unable to resolve host "webservice.hcxwords.eu": No address associated with > hostname
最佳答案
最有可能的原因是模拟器与 Web 服务器位于同一网络(从而允许解析主机名),但设备却不然。
两种可能的解决方案:
使用 VPN 将您的设备连接到网络(假设网络支持 VPN)。 JunosPulse 是一款优秀的免费 VPN 客户端,您可以下载到您的 Android 设备上。
将 Web 服务器暴露在防火墙之外。然而,您可能无法做到这一点。选项 1 通常是您的最佳选择。
另一种可能是某些东西阻止设备解析服务器名称。尝试通过IP地址访问服务器,例如:
String url = "http://<ip_address_of_server>/WebserviceHCX.asmx";
关于java - 只能从 Android 模拟器访问 SOAP 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20103253/