[英]Consuming asp.net XML web service in android using ksoap2
I am trying to consume an ASP.NET web service in Android with Ksoap2. 我正在尝试使用Ksoap2在Android中使用ASP.NET Web服务。 Web service returns an XML response in browser like the following:
Web服务在浏览器中返回XML响应,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
-<Students>
-<student>
<stdName>Maria</stdName>
<stdRollNo>1</stdRollNo>
-</student>
-<student>
<stdName>Anna</stdName>
<stdRollNo>2</stdRollNo>
-</student>
-</Students>
On calling this service in Android with piece of code given below I get an exception: 在Android中使用下面给出的代码调用此服务时,我得到一个例外:
Object response=null
try {
httpTransport.call(soap_action,envelope);
response=envelope.bodyIn;
Log.v("response is ", response.toString());
} catch(Exception e) {
e.getMessage();
}
Exception: 例外:
response: "SoapFault" -faultcode:'soap:server' faultstrings: 'Server was not able to process request' object reference was not set to instance of an object"
I have made changes to that piece of code based on different suggestions given on Stackoverflow 我根据Stackoverflow上给出的不同建议对这段代码进行了更改
SoapObject response=null
try{
httpTransport.call(soap_action,envelope);
response=(SoapObject)envelope.bodyIn;
Log.v("response is ", response.toString());
} catch(Exception e){
e.getMessage();
}
and 和
SoapObject response=null
try{
httpTransport.call(soap_action,envelope);
response=(SoapObject)envelope.getresponse();
Log.v("response is ", response.toString());
} catch(Exception e){
e.getMessage();
}
But the exception then changes to: 但例外情况则变为:
java.lang.classCastexception
Any ideas as to what's happening? 关于发生了什么的任何想法?
You need to : 1/ Specify your method name (defined in your ASP.Net code) : webMethName. 您需要:1 /指定您的方法名称(在ASP.Net代码中定义):webMethName。 2/ Use SoapPrimitive instead of SoapObject, it's where you'll get the web service respone.
2 /使用SoapPrimitive而不是SoapObject,它就是你将获得Web服务的地方。
Please edit your code block with the following : 请使用以下代码编辑您的代码块:
String resText = "";
try {
// Invoke web service
httpTransport.call(soap_action + webMethName, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
// Assign it to resText variable
resText = response.toString();
}
catch (Exception e) {
//Print error
e.printStackTrace();
//Assign error message to resText
resText = "Error occured";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.