无法使用Ksoap2调用soap webservice

时间:2013-12-06 07:17:26

标签: 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.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();
                                }

编辑:

12-06 12:40:59.988: I/(27306): code====500
12-06 12:40:59.988: W/System.err(27306): java.io.FileNotFoundException: http://www.bmtqs.com.au/DepreciationCalculatorService.svc/basic
12-06 12:40:59.996: W/System.err(27306):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
12-06 12:40:59.996: W/System.err(27306):    at com.bmtqs.bmttaxcalc.MainScreen$calc.doInBackground(MainScreen.java:1015)
12-06 12:40:59.996: W/System.err(27306):    at com.bmtqs.bmttaxcalc.MainScreen$calc.doInBackground(MainScreen.java:1)
12-06 12:40:59.996: W/System.err(27306):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-06 12:40:59.996: W/System.err(27306):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-06 12:40:59.996: W/System.err(27306):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-06 12:40:59.996: W/System.err(27306):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-06 12:40:59.996: W/System.err(27306):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-06 12:40:59.996: W/System.err(27306):    at java.lang.Thread.run(Thread.java:856)

EDIT2: 我也尝试过这种方式,但这也行不通。

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();
                            }

编辑3: 使用kso​​ap时的logcat

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)

1 个答案:

答案 0 :(得分:0)

此错误表示您未提供正确的Url,Soap Action或方法名称。仔细检查它们,我想你会发现问题所在。