Android 无法使用Ksoap2调用soap Web服务
调用soap服务时,我收到FileNotFoundException,响应代码为500,服务在IOS中运行良好,这是我的代码,我使用的是ksoap2-android-assembly-3.1.1-jar-with-dependencies.jar,我是Ksoap新手,请帮助Android 无法使用Ksoap2调用soap Web服务,android,android-ksoap2,Android,Android Ksoap2,调用soap服务时,我收到FileNotFoundException,响应代码为500,服务在IOS中运行良好,这是我的代码,我使用的是ksoap2-android-assembly-3.1.1-jar-with-dependencies.jar,我是Ksoap新手,请帮助 try { URL mUrl = new URL( "http://www.bmtqs.com
try {
URL mUrl = new URL(
"http://www.bmtqs.com.au/DepreciationCalculatorService.svc/basic");
HttpURLConnection conn = (HttpURLConnection)mUrl.openConnection();
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.addRequestProperty("SOAPAction","http://tempuri.org/IDepreciationCalculatorService/Calculate");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn
.getOutputStream());
String body = "<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"
+ " xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>"
+ " <SOAP-ENV:Body> <Calculate xmlns='http://tempuri.org/'> <detail_ xmlns:a='http://schemas.datacontract.org/2004/07/BmtWebsite.DepreciationCalculator.Business' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>"
+ "<a:BuildingType>" + "Apartment/Unit" + "</a:BuildingType>"
+ "<a:City>" + "Sydney" + "</a:City>"
+ "<a:Construction>" + "2013-07-01" + "</a:Construction>"
+ "<a:Finish>" + "Medium" + "</a:Finish>"
+ "<a:FloorArea>" + "120" + "</a:FloorArea>"
+ "<a:IsSquares>" + "false" + "</a:IsSquares>"
+ "<a:Levels>" + "10" + "</a:Levels>"
+ "<a:MaxFloorArea>" + "5000" + "</a:MaxFloorArea>"
+ "<a:MaxLevels>" + "100" + "</a:MaxLevels>"
+ "<a:MinFloorArea>" + "10" + "</a:MinFloorArea>"
+ "<a:PropertyType>" + "Residential" + "</a:PropertyType>"
+ "<a:Purchase>" + "2013-07-01" + "</a:Purchase>"
+ "<a:Units>" + "1" + "</a:Units>"
+ "</detail_>"
+ "</Calculate>"
+ "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
wr.write(body);
wr.flush();
// Get the response
Log.i("", "code===="+conn.getResponseCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(conn
.getInputStream()));
String mResult = "";
String line;
while ((line = rd.readLine()) != null) {
mResult += line;
}
wr.close();
rd.close();
return mResult;
} catch (Exception e) {
e.printStackTrace();
}
编辑2:
我也试过这种方法,但也不起作用
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.bmtqs.com.au/DepreciationCalculatorService.svc/basic";
private final String SOAP_ACTION = "http://tempuri.org/IDepreciationCalculatorService/Calculate";
private final String METHOD_NAME = "Calculate";
try{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("BuildingType","Apartment/Unit");
request.addProperty("City", ""+"Sydney");
request.addProperty("Construction", "2013-07-01");
request.addProperty("Finish", "Medium");
request.addProperty("FloorArea", "120");
request.addProperty("IsSquares", "false");
request.addProperty("Levels", "10");
request.addProperty("MaxFloorArea", "5000");
request.addProperty("MaxLevels", "100");
request.addProperty("MinFloorArea", "10");
request.addProperty("PropertyType", "Residential");
request.addProperty("Purchase", "2013-07-01");
request.addProperty("Units", "1");
// request.addProperty("FloorArea",floor_area.getText().toString().trim() );
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
Element[] header = new Element[1];
header[0] = new Element().createElement(NAMESPACE, "a");
header[0].addChild(Node.TEXT, "HeaderTextContent");
envelope.headerOut = header;
// envelope.implicitTypes=true;
// envelope.setAddAdornments(false);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,30000);
androidHttpTransport.debug=true;
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.call(SOAP_ACTION, envelope);
// SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
// response = (SoapObject) envelope.getResponse();
res=envelope.getResponse().toString();
Log.i("", "cal response==="+envelope.getResponse());
}
catch(Exception e){
e.printStackTrace();
}
此错误表明您没有提供正确的Url、Soap操作或方法名称。仔细检查一下,我想你会发现问题的 您的代码中没有使用ksoap2。也许这就是问题所在?@XaverKapeller我试过了,但什么都没用。当你使用ksoap2时,到底是什么不起作用?使用HttpUrlConnection简单地发送soap信封add strong很少起作用,这不是一个好的解决方案?如果你得到一个例外,请张贴logcati检查,但这些事情都没有错..你也可以检查,这是服务的url。。。我正在使用此服务的计算方法
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.bmtqs.com.au/DepreciationCalculatorService.svc/basic";
private final String SOAP_ACTION = "http://tempuri.org/IDepreciationCalculatorService/Calculate";
private final String METHOD_NAME = "Calculate";
try{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("BuildingType","Apartment/Unit");
request.addProperty("City", ""+"Sydney");
request.addProperty("Construction", "2013-07-01");
request.addProperty("Finish", "Medium");
request.addProperty("FloorArea", "120");
request.addProperty("IsSquares", "false");
request.addProperty("Levels", "10");
request.addProperty("MaxFloorArea", "5000");
request.addProperty("MaxLevels", "100");
request.addProperty("MinFloorArea", "10");
request.addProperty("PropertyType", "Residential");
request.addProperty("Purchase", "2013-07-01");
request.addProperty("Units", "1");
// request.addProperty("FloorArea",floor_area.getText().toString().trim() );
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
Element[] header = new Element[1];
header[0] = new Element().createElement(NAMESPACE, "a");
header[0].addChild(Node.TEXT, "HeaderTextContent");
envelope.headerOut = header;
// envelope.implicitTypes=true;
// envelope.setAddAdornments(false);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,30000);
androidHttpTransport.debug=true;
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.call(SOAP_ACTION, envelope);
// SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
// response = (SoapObject) envelope.getResponse();
res=envelope.getResponse().toString();
Log.i("", "cal response==="+envelope.getResponse());
}
catch(Exception e){
e.printStackTrace();
}
12-06 13:16:05.793: W/System.err(32177): SoapFault - faultcode: 'a:InternalServiceFault' faultstring: 'Object reference not set to an instance of an object.' faultactor: 'null' detail: org.kxml2.kdom.Node@41d3e658
12-06 13:16:05.816: W/System.err(32177): at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:141)
12-06 13:16:05.816: W/System.err(32177): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
12-06 13:16:05.816: W/System.err(32177): at org.ksoap2.transport.Transport.parseResponse(Transport.java:118)
12-06 13:16:05.816: W/System.err(32177): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:272)
12-06 13:16:05.816: W/System.err(32177): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
12-06 13:16:05.816: W/System.err(32177): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
12-06 13:16:05.824: W/System.err(32177): at com.bmtqs.bmttaxcalc.MainScreen$calc.doInBackground(MainScreen.java:963)
12-06 13:16:05.824: W/System.err(32177): at com.bmtqs.bmttaxcalc.MainScreen$calc.doInBackground(MainScreen.java:1)
12-06 13:16:05.824: W/System.err(32177): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-06 13:16:05.824: W/System.err(32177): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-06 13:16:05.824: W/System.err(32177): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-06 13:16:05.824: W/System.err(32177): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-06 13:16:05.824: W/System.err(32177): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-06 13:16:05.832: W/System.err(32177): at java.lang.Thread.run(Thread.java:856)