Java 访问SOAP服务只能从Android Emulator工作
我正在尝试从我的android应用程序访问SOAP服务Java 访问SOAP服务只能从Android Emulator工作,java,android,web-services,soap,Java,Android,Web Services,Soap,我正在尝试从我的android应用程序访问SOAP服务 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:
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 ");
}
我正在使用测试我的请求,因此我最终得到了以下请求:
发送的请求头:
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>
请求数据:
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>
测试
因为这在我的android应用程序中运行得非常好(200行),所以我只想在我的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 ");
}
最终字符串SOAPRequestXML=“+deviceID+”“+refCode2+”;
字符串url=”http://webservice.hcxwords.eu/WebserviceHCX.asmx";
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
addHeader(“内容类型”,“text/xml;charset=UTF-8”);
httppost.addHeader(“SOAPAction”、“hcxwords/installAcknowledge”);
httppost.addHeader(“主机”,“webservice.hcxwords.eu”);
httppost.addHeader(“接受字符集”、“utf-8”);
addHeader(“接受”,“text/xml,application/text+xml,application/soap+xml”);
Stringse;
试一试{
se=新的StringEntity(SOAPRequestXML,HTTP.UTF_8);
setContentType(“text/xml;charset=UTF-8”);
se.setContentEncoding(“utf-8”);
httppost.setEntity(se);
//执行HTTP Post请求
基本httpResponse httpResponse=
(BasicHttpResponse)httpclient.execute(httppost);
返回httpResponse;
}捕获(客户端协议例外e){
e、 printStackTrace();
抛出新的非法状态异常(“ClientProtocolException”);
}捕获(IOE异常){
e、 printStackTrace();
抛出新的非法状态异常(“IOException”);
}
这在我的模拟器(200 OK)中非常有效,在我的Nexus 4上它是(404),在我的三星平板电脑上它抛出IOException:
IOException:无法解析主机“webservice.hcxwords.eu”:没有与>主机名关联的地址
最可能的原因是仿真器与web服务器位于同一网络上(因此允许解析主机名),但设备不在 两种可能的解决办法:
String url = "http://<ip_address_of_server>/WebserviceHCX.asmx";
stringurl=”http:///WebserviceHCX.asmx";
是,但仿真器可能位于同一防火墙后面,在这种情况下,它可以解析名称。很明显,设备无法解析名称。请尝试更改您的URL,以便它通过IP地址而不是名称引用服务器。是的,我现在知道了。。。现在到处都能用了!谢谢。