【发布时间】:2012-03-04 14:37:34
【问题描述】:
我正在开发一个 android 应用程序,在该应用程序中我必须从 .Net webservice 获取数据最初的 webservice 方法返回简单的字符串。这很好我的代码工作正常并且可以顺利地获取字符串。但是当方法返回对象时我的问题就开始了,我知道对象以 XML 格式保存数据。我还通过调试代码确认了这一点,结果对象保存了下面给出的数据。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<UserInfoResponse xmlns="http://tempuri.org/">
<UserInfoResult>
<UserName>Himanshu</UserName>
<Email>Himanshu@XXXXXXXX.com</Email>
</UserInfoResult>
</UserInfoResponse>
</soap:Body>
</soap:Envelope>
我使用网络服务的代码是:-
public void objData(){
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
Log.d("request", request.toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
Log.d("envelope", envelope.toString());
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
Log.d("envelope", envelope.toString());
HttpTransportSE aht = new HttpTransportSE(URL);
aht.debug=true;
Log.d("aht", aht.toString());
try
{
aht.call(OBJ_SOAP_ACTION, envelope);
SoapPrimitive results = (SoapPrimitive)envelope.getResponse();
System.out.println("results="+results);
tv4.setText(""+results);
}
catch (Exception e)
{
tv4.setText(e.getClass().toString());
Log.d("Error",e.getClass().toString());
}
}
对象results 保存xml 数据。现在我的问题是,当我使用代码tv4.setText(""+results); 打印该对象的数据时,它给了我class java.lang.ClassCastException。我知道这不是正确的方法获取对象的 xml 数据,我必须解析它。但我不知道如何解析对象。所以请帮我解析 xml 包含的对象。任何帮助将不胜感激。提前谢谢。
【问题讨论】:
标签: android web-services xml-parsing