尝试调用wcf Web服务时出现XMLPullParserException

时间:2012-12-04 19:09:02

标签: android wcf ksoap2 xmlpullparser

我在连接到我的网络服务时遇到问题。我已经关注了几个教程,尤其是http://drclawx.wordpress.com/2011/06/24/77/,并创建了一个简单的Wcf Hello World Web服务。我已经下载了网站上推荐的ksoap2 jar,但我遇到了一些问题。我已经下载了几个更高版本,但我可以开始工作的版本是2.5.8版本。当我打电话给自己的网络服务时,我得到了一个XMLPullParserException。我的网络服务将用于检查登录凭据,它有一个名为' UploadLogin'它需要用户名和密码。以下是我的代码

private class webService extends AsyncTask<String, Void, Void> {

    final String METHOD_NAME = "UploadLogin";
    final String NAMESPACE = "http://tempuri.org/";     
    final String URL = "http://10.0.2.2:52762/UploadService.svc";
    final String SOAP_ACTION = "http://tempuri.org/IUploadService/UploadLogin";

    // For the web service method: public String getLocations(String inputLocation))
    @Override
    protected Void doInBackground(String... entry) {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("username", "a");
        request.addProperty("password", "a");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object response = (Object) envelope.getResponse();
            System.out.println("Login - " + response.toString());
        } 
        catch (Exception exception) {
            exception.printStackTrace();   
        }

        return null;
    } 
}

我得到的错误如下。有人可以帮忙或建议我如何解决这个问题。

12-04 18:50:44.317: W/System.err(1704): org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT 
12-04 18:50:44.317: W/System.err(1704): --uuid:e537e5f2...@3:13 in java.io.InputStreamReader@41325c88) 
12-04 18:50:44.326: W/System.err(1704):     at org.kxml2.io.KXmlParser.next(KXmlParser.java:426)
12-04 18:50:44.336: W/System.err(1704):     at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
12-04 18:50:44.336: W/System.err(1704):     at org.kxml2.io.KXmlParser.nextTag(KXmlParser.java:2029)
12-04 18:50:44.346: W/System.err(1704):     at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:126)
12-04 18:50:44.356: W/System.err(1704):     at org.ksoap2.transport.Transport.parseResponse(Transport.java:96)
12-04 18:50:44.356: W/System.err(1704):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:189)
12-04 18:50:44.366: W/System.err(1704):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
12-04 18:50:44.376: W/System.err(1704):     at prometric.myitemwriter.ViewItemBankActivity$ws_MIR.doInBackground(ViewItemBankActivity.java:538)
12-04 18:50:44.376: W/System.err(1704):     at prometric.myitemwriter.ViewItemBankActivity$ws_MIR.doInBackground(ViewItemBankActivity.java:1)
12-04 18:50:44.396: W/System.err(1704):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-04 18:50:44.426: W/System.err(1704):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-04 18:50:44.436: W/System.err(1704):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-04 18:50:44.516: W/System.err(1704):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-04 18:50:44.516: W/System.err(1704):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-04 18:50:44.526: W/System.err(1704):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-04 18:50:44.536: W/System.err(1704):     at java.lang.Thread.run(Thread.java:856)

3 个答案:

答案 0 :(得分:2)

Instead of using kSOAP library, you can do it with built-in apache classes like HttpClient, HttpGet, HttpPost, etc.

For example: You want to send below JSON object:

{
   "email":"test@test.com",
   "password":"test"
}
then you can set your request object by using setEntity():

final String body = String.format("{\"email\":\"%s\",\"password\":\"%s\"}", s1, s2);

HttpClient client = new DefaultHttpClient();
HttpPost postMethod = new HttpPost(webServiceUrl);
postMethod.setEntity(new StringEntity(body, "utf-8"));

答案 1 :(得分:0)

对于任何有兴趣的人,我收到此错误的原因是因为messageEncoding正在使用MTOM,所以我将其更改为Text并且它正常工作 - messageEncoding =“Text”

答案 2 :(得分:-1)

在我的情况下,由于缺少必需的方法参数而发生错误:

    request.addProperty("cpf", "111111111");