如何在android中使用Svc WCF服务

时间:2013-05-07 05:05:14

标签: android web-services ksoap xmlpullparser

Wsdl Url:http://xxx.xxx.xxx/UserService.svc

我用Ksoap来点击服务......

这是我的代码:

try {
                request = new SoapObject("http://tempuri.org/", "GetUserDetailsByUserName");

                request.addProperty("username", "Prasad");
                envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);
                Log.i("Wsdl Response", "Wsdl Response:");
                // request.setHeader("Content-type", "application/xml");
                // Proxy px = new Proxy();

                // ?singleWsdl
                httpreq = new HttpTransportSE("http://xxx.xxx.xxx/UserService.svc");
                httpreq.debug = true;
                httpreq.call("http://tempuri.org/xxxe/GetUserDetailsByUserName", envelope);
                Log.i("Wsdl Response", ":Wsdl Response:123");
                String xml = (String) envelope.getResponse().toString();
                SoapPrimitive results = (SoapPrimitive) envelope.getResponse();
                /*
                 * if (envelope.bodyIn instanceof SoapFault) { String errormessage = ((SoapFault) envelope.bodyIn).faultstring; throw new Exception(errormessage); } IncomeLeadXMLPharsing parse = new IncomeLeadXMLPharsing(); parse.parse(xml); ResultVecotor = parse.getVector(); mHandler.post(setValuesToUi);
                 */
                Log.i("Wsdl Response", ":Wsdl Response:" + xml);
                if (progress != null) {
                    if (progress.isShowing()) {
                        progress.dismiss();
                    }
                }


            }
            catch (UnknownHostException e) {
                if (progress != null) {
                    if (progress.isShowing()) {
                        progress.dismiss();
                    }
                }
                e.printStackTrace();
            }
            catch (ConnectException e) {
                if (progress != null) {
                    if (progress.isShowing()) {
                        progress.dismiss();
                    }
                }
                e.printStackTrace();
            }
            catch (SocketException e) {
                if (progress != null) {
                    if (progress.isShowing()) {
                        progress.dismiss();
                    }
                }
                e.printStackTrace();
            }
            catch (Exception e) {
                if (progress != null) {
                    if (progress.isShowing()) {
                        progress.dismiss();
                    }
                }
                e.printStackTrace();
                System.out.print("EXCEPTION" + e);
            }

但我在log cat中得到了这个例外:即使经过大量尝试,我也能找到解决方案

05-07 10:29:22.861: WARN/System.err(635): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <html>@3:44 in java.io.InputStreamReader@413016e0) 
05-07 10:29:22.861: WARN/System.err(635):     at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)
05-07 10:29:22.861: WARN/System.err(635):     at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:127)
05-07 10:29:22.861: WARN/System.err(635):     at org.ksoap2.transport.Transport.parseResponse(Transport.java:100)
05-07 10:29:22.871: WARN/System.err(635):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:195)
05-07 10:29:22.871: WARN/System.err(635):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
05-07 10:29:22.871: WARN/System.err(635):     at com.myApp.JsonService$SampleWsdlServiceAsync.doInBackground(JsonService.java:291)
05-07 10:29:22.871: WARN/System.err(635):     at com.myApp.JsonService$SampleWsdlServiceAsync.doInBackground(JsonService.java:1)
05-07 10:29:22.871: WARN/System.err(635):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-07 10:29:22.881: WARN/System.err(635):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-07 10:29:22.881: WARN/System.err(635):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-07 10:29:22.881: WARN/System.err(635):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-07 10:29:22.894: WARN/System.err(635):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-07 10:29:22.894: WARN/System.err(635):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-07 10:29:22.902: WARN/System.err(635):     at java.lang.Thread.run(Thread.java:856)

如果有人帮助我会很棒。

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试更改这些行:

我认为您的通话网址不正确:

try {                

 androidHttpTransport.call("http://tempuri.org/GetUserDetailsByUserName", Envelope);
} catch (IOException e) {
 Log.d("ErrorApp", e.getMessage().toString());
} catch (XmlPullParserException e) {
 Log.d("ErrorApp", e.getMessage().toString());

}

SoapPrimitive response= (SoapPrimitive) Envelope.getResponse();