【问题标题】:Unmarshalling soap response in Android在 Android 中解组肥皂响应
【发布时间】:2014-09-05 15:48:30
【问题描述】:

我有两个应用程序,客户端应用程序(移动应用程序)具有 android 和服务器应用程序具有 Java 编程语言。 我已经使用 jaxb 来编组我的自定义列表。

          Keyword objKeyword=new Keyword();
          objKeyword.setKeywordList(keywordList); //keywordList=new ArrayList<Keyword>();

          JAXBContext jc = JAXBContext.newInstance(Keyword.class);
          Marshaller mar = jc.createMarshaller();
          mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

          sw = new StringWriter();

          mar.marshal(objKeyword, sw);

我通过编写以下代码行在我的 android 应用程序中得到响应,

     // Create request by specifying Namespace and Operation Name
      SoapObject request = new SoapObject(WSDL_NAMESPACE, "getList");

       //   Creating envelope
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;

      //    Setting output SOAP object
      envelope.setOutputSoapObject(request);

       //   Creating HTTP call object
       HttpTransportSE androidHttpTransport = new HttpTransportSE(WSDL_URL);

        //  Invoking web service
        androidHttpTransport.call(WSDL_NAMESPACE + "getList", envelope);

        //  Getting the response
        SoapObject response = (SoapObject) envelope.bodyIn;

在我的回复中,我得到了以下格式的自定义列表,

<My_list>
  <keyList>
    <id>1</id>
    <name>Key1</name>
   </keyList>

  <keyList>
    <id>2</id>
    <name>Key2</name>
   </keyList>
</My_list>

现在,我想在我的 android 客户端应用程序中解组肥皂响应。 我读过JAXB不能与android一起使用,而是可以使用SAX解析器进行XML解析。

请帮忙。

【问题讨论】:

    标签: android soap jaxb saxparser unmarshalling


    【解决方案1】:

    没有必要解组它, 我可以使用以下代码行在我的响应中获取整个列表,

    //  Getting the response
    SoapObject response = (SoapObject) envelope.bodyIn;
    

    然后我可以使用 , 获取列表计数,

    int iResponseCount=response.getPropertyCount();
    

    然后是for循环

        List<Keys> lstKeys=new ArrayList<Keys>();
        for (int i = 0; i < iResponseCount; i++) {
                SoapObject responseKey =(SoapObject) response.getProperty(i);
    
                Keys objKeys =new Keys();
                objKeys.setId(responseKey.getProperty(0).toString());
                objKeys.setName(responseKey.getProperty(1).toString());
    
                lstKeys.add(objKeys);
        }
    

    PS:我还将服务器应用程序函数(用java编写)的返回类型从String更改为,

    List<Keys> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      相关资源
      最近更新 更多