我是KSOAP2的新手。显然,我无法为Android中的网络服务调用获得正确类型的信封。
这是我的代码:
private static final String METHOD_NAME = "wmtdEncApaLuces";
private static final String SOAP_ACTION = "http://tempuri.org/wsLucesAutomaticas/wmtdEncApaLuces";
private static final String NAMESPACE = "http://tempuri.org";
private static final String URL = "http://softour.ddns.net:8083/wsLucesAutomaticas.svc/basic";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo strIp =new PropertyInfo();
strIp.setName("strIp");
strIp.setValue("192.168.1.121");
strIp.setType(String.class);
request.addProperty(strIp);
PropertyInfo boolApaEnc =new PropertyInfo();
boolApaEnc.setName("boolApaEnc");
boolApaEnc.setValue(false);
boolApaEnc.setType(boolean.class);
request.addProperty(boolApaEnc);
PropertyInfo strDispositivo =new PropertyInfo();
strDispositivo.setName("strDispositivo");
strDispositivo.setValue("Nexus 6");
strDispositivo.setType(String.class);
request.addProperty(strDispositivo);
PropertyInfo strUsuario =new PropertyInfo();
strUsuario.setName("strUsuario");
strUsuario.setValue("John");
strUsuario.setType(String.class);
request.addProperty(strUsuario);
PropertyInfo intTipoUs =new PropertyInfo();
intTipoUs.setName("intTipoUs");
intTipoUs.setValue(1);
intTipoUs.setType(int.class);
request.addProperty(intTipoUs);
PropertyInfo intPuerto =new PropertyInfo();
intPuerto.setName("intPuerto");
intPuerto.setValue(85);
intPuerto.setType(int.class);
request.addProperty(intPuerto);
PropertyInfo strIpConexionArduino =new PropertyInfo();
strIpConexionArduino.setName("strIpConexionArduino");
strIpConexionArduino.setValue("192.168.1.120");
strIpConexionArduino.setType(String.class);
request.addProperty(strIpConexionArduino);
PropertyInfo intPuertoConexionArduino =new PropertyInfo();
intPuertoConexionArduino.setName("intPuertoConexionArduino");
intPuertoConexionArduino.setValue(1234);
intPuertoConexionArduino.setType(int.class);
request.addProperty(intPuertoConexionArduino);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = false;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
Log.d("myTag", "Request: " + androidHttpTransport.requestDump);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
// Log.d("myTag", "Response: " + response.toString());
}
catch(Exception e) {
e.printStackTrace();
}
为SoapSerializationEnvelope生成的XML是:
<?xml version="1.0" encoding="UTF-8"?>
<v:Envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<v:Header />
<v:Body>
<n0:wmtdEncApaLuces xmlns:n0="http://tempuri.org" id="o0" c:root="1">
<strIp i:type="d:string">192.168.1.121</strIp>
<boolApaEnc i:type="d:boolean">false</boolApaEnc>
<strDispositivo i:type="d:string">Nexus 6</strDispositivo>
<strUsuario i:type="d:string">Alonso</strUsuario>
<intTipoUs i:type="d:int">1</intTipoUs>
<intPuerto i:type="d:int">85</intPuerto>
<strIpConexionArduino i:type="d:string">192.168.1.120</strIpConexionArduino>
<intPuertoConexionArduino i:type="d:int">1234</intPuertoConexionArduino>
</n0:wmtdEncApaLuces>
</v:Body>
</v:Envelope>
我还使用SoapUI对其进行了测试,并生成了这个XML,这与KSOAP生成的XML不同。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:wmtdEncApaLuces>
<!--Optional:-->
<tem:strIp>192.168.1.121</tem:strIp>
<tem:bolApaEnc>false</tem:bolApaEnc>
<!--Optional:-->
<tem:strDispositivo>Nexus 6</tem:strDispositivo>
<!--Optional:-->
<tem:strUsuario>John</tem:strUsuario>
<tem:intTipoUs>1</tem:intTipoUs>
<tem:intPuerto>85</tem:intPuerto>
<!--Optional:-->
<tem:strIpConexionArduino>192.168.1.120</tem:strIpConexionArduino>
<tem:intPuertoConexionArduino>1234</tem:intPuertoConexionArduino>
</tem:wmtdEncApaLuces>
</soapenv:Body>
</soapenv:Envelope>
这里的定义为http://softour.ddns.net:8083/wsLucesAutomaticas.svc/basic?singleWSDL
感谢任何帮助。
答案 0 :(得分:1)
关于命名空间,看看这个:
<v:Envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
现在,看看这个:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
命名空间不同。尝试在KSOAP Client中添加正确的命名空间,或者更好,在.NET Web服务中正确处理请求。
下一个代码Works,尝试使用HttPost代替KSOAP:
HttpPost httppost = new HttpPost(URL);
StringEntity se;
String SOAPRequestXML = "";
try {
SOAPRequestXML="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/><soapenv:Body><tem:wmtdEstadoActuadoIluminacion><tem:strIp>192.168.1.121</tem:strIp><tem:strDispositivo>Nexus 6</tem:strDispositivo><tem:strUsuario>Alonso</tem:strUsuario><tem:intTipoUs>1</tem:intTipoUs><tem:intPuerto>85</tem:intPuerto><tem:strIpConexionArduino>192.168.1.120</tem:strIpConexionArduino><tem:intPuertoConexionArduino>1234</tem:intPuertoConexionArduino></tem:wmtdEstadoActuadoIluminacion></soapenv:Body></soapenv:Envelope>";
se = new StringEntity(SOAPRequestXML, HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setHeader("Accept-Charset","utf-8");
httppost.setHeader("soapaction", SOAP_ACTION);
httppost.setEntity(se);
HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
Log.e("RESPONSE: ", EntityUtils.toString(resEntity));
} catch (Exception e) {
e.printStackTrace();
}
注意:请记住AsyncTask以执行请求。