调用Web服务时出错

时间:2012-05-14 09:51:02

标签: java android web-services ksoap2

我已经完成了使用.net c#开发的asmx,它运行良好

但是当我需要使用java android调用时,我发现了一个错误 错误:java.lang.ClassCastException:org.ksoap2.serialization.SoapObject无法强制转换为org.ksoap2.serialization.SoapPrimitive

结果显示在我的asmx:

<customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<customerID>1</customerID>
<companyName>ABC</companyName>
<contactName>Jack</contactName>
<customerID>2</customerID>
<companyName>SS Company</companyName>
<contactName>Mary</contactName>
</customer>

我的java cs:

        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView)findViewById(R.id.textView1);

        SoapObject Request = new SoapObject (NAMESPACE, METHOD_NAME);
        Request.addProperty("custID", "member");

        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);

        AndroidHttpTransport aht = new AndroidHttpTransport(URL);

        try
        {
         aht.call(SOAP_ACTION, soapEnvelope);
        SoapObject resultString = (SoapObject) soapEnvelope.getResponse();
        tv.setText(resultString.toString());
        }
        catch(Exception e)
        {
            tv.setText(e.toString());          
        }                 
    }

如果我使用http://www.w3schools.com/webservices/tempconvert.asmx

,上面的代码就会正常运行

3 个答案:

答案 0 :(得分:1)

我已经解决了它

           SoapObject resultString = (SoapObject) soapEnvelope.getResponse();           

            String addon = "";
            for(int i =0;i<resultString.getPropertyCount();i++)
            {                            
                SoapObject array2 = (SoapObject) resultString .getProperty(i);              
                addon = (addon + "ID = " + array2.getProperty(0).toString() + array2.getProperty(1).toString() + array2.getProperty(2).toString() + "\n");  
            }
            tv.setText(addon.toString());

答案 1 :(得分:0)

1)将 userType 更改为 custID 。这是你的错。

测试this

在这些情况下,ksoap2序列化程序无法将返回的字符串转换为有效的xml字符串。

2)试试这个:

aht.call(SOAP_ACTION, soapEnvelope);
SoapObject resultString = (SoapObject) soapEnvelope.getResponse();
tv.setText(resultString.getPropertyAsString("customerID"));  

答案 2 :(得分:0)

好像你必须解析一个复杂的对象。您必须创建一个实现Customer接口的KvmSerializable类。看看这个:http://seesharpgears.blogspot.com.es/2010/10/ksoap-android-web-service-tutorial-with.html