如何通过Android发送soap请求?
我如何向此服务发送请求?
<?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>
<total xmlns="http://tempuri.org/">
<validationRequest>
<Name>string</Name>
<num1>int</num1>
<num2>int</num2>
</validationRequest>
</total>
</soap:Body>
</soap:Envelope>
安卓代码:
private static final String NAMESPACE ="http://tempuri.org/";
private static final String SOAP_ACTION ="http://tempuri.org/total";
private static final String URL ="http://10.0.2.2:1743/Service1.asmx";
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
tv.setText(ws());
}
private String ws() {
String result = "";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
System.out.println("ohaissxh");
PropertyInfo quotesProperty = new PropertyInfo();
request.addProperty("Name","Nas");
request.addProperty("num1",6);
request.addProperty("num2",5);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
System.out.println("dfdjsssf");
if(envelope.getResponse()!=null){
//SoapObject response = (SoapObject)envelope.bodyIn;
Object response = envelope.getResponse();
result = response.toString();
}
} catch (Exception e) {
result = e.getMessage();
}
return result;
}
How can I send a request to this service?
<?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>
<total xmlns="http://tempuri.org/">
<validationRequest>
<Name>string</Name>
<num1>int</num1>
<num2>int</num2>
</validationRequest>
</total>
</soap:Body>
</soap:Envelope>
Android code:
private static final String NAMESPACE ="http://tempuri.org/";
private static final String SOAP_ACTION ="http://tempuri.org/total";
private static final String URL ="http://10.0.2.2:1743/Service1.asmx";
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
tv.setText(ws());
}
private String ws() {
String result = "";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
System.out.println("ohaissxh");
PropertyInfo quotesProperty = new PropertyInfo();
request.addProperty("Name","Nas");
request.addProperty("num1",6);
request.addProperty("num2",5);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
System.out.println("dfdjsssf");
if(envelope.getResponse()!=null){
//SoapObject response = (SoapObject)envelope.bodyIn;
Object response = envelope.getResponse();
result = response.toString();
}
} catch (Exception e) {
result = e.getMessage();
}
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的 SOAP 请求中,
是一种复杂的请求类型,您根本无法像通常对原始类型那样在request
中添加属性。您需要创建一个类,它
扩展 Vector
并实现 KVMSerialized
。您的类将包含您要发送的参数,并且您需要将类的对象添加到
请求
中。示例代码段可以在官方 KSOAP 文档< /strong>。
In your SOAP request,
<validationRequest>
is a complex request type and you simply cannot add the property in therequest
, as usually done for primitive types.You need to create a class which
extends Vector
andimplements KVMSerializable
.Your class would contain the parameters that you want to send and you need to add the object of the class to the
request
.Sample code snippet can be found in the official KSOAP documentation.