【问题标题】:Query about xml parsing in androidandroid中关于xml解析的查询
【发布时间】: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


    【解决方案1】:

    要从soap中获取复杂的数据对象,通过实现KvmSerializable将对象定义为KvmSerializale,见链接:

    http://seesharpgears.blogspot.in/2010/10/ksoap-android-web-service-tutorial-with.html

    http://seesharpgears.blogspot.in/2010/11/implementing-ksoap-marshal-interface.html

    【讨论】:

      【解决方案2】:

      您不能将 SoapPrimitive 用于复杂对象,您必须像下面这样转换响应

      SoapObject response = (SoapObject) envelope.getResponse();
      

      然后,如果您想将其转换为实体对象(类似于您通过 .net Web 服务发送的对象),您必须这样做。

      实体类

      public class EmLogin {
      
      private String _id;
      
      private String _pwd;
      
      
      public EmLogin() {
      }
      
      public String getId() {
          return _id;
      }
      
      public void setId(String id) {
          this._id = id;
      }
      
      public String getPwd() {
          return _pwd;
      }
      
      public void setPwd(String pwd) {
          this._pwd = pwd;
      }
      
      //this is the place you are setting the SoapObject as a param
      public EmLogin(SoapObject so) throws ParseException {
      
          this._id = so.getProperty("Id").toString();// these are the properties of your xml objs
          this._pwd = so.getProperty("PasswordHash").toString();
      
      }}
      

      将检索到的soapobject设置为实体类的方式如下。

      EmLogin = null;
      if (yourSoapObject!= null) {
          // here you are parsing the soapobject to the emlogin object as a param
          emLogin = new EmLogin(yourSoapObject);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-24
        • 2011-03-30
        • 2015-04-17
        • 1970-01-01
        • 2012-02-28
        • 1970-01-01
        • 1970-01-01
        • 2012-11-21
        相关资源
        最近更新 更多