如何解析android中的SoapObject

时间:2012-02-12 13:20:31

标签: android web-services

我正在使用webservices来为应用程序从外部服务器获取数据。我能够连接到服务器,发送我的SOAP请求,并能够将数据作为SoapObject返回到我的android类。 在这里,我无法解析此SoapObject以检索来自Web服务的值(作为字符串)。 我的代码在这里:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        mInflater =  (LayoutInflater)getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
       System.out.println("Entered "+getClass().getSimpleName());
       //Calling the web service to get the documents list
       callService("getDocumentsList","http://wservice.friedmaggy.com/getDocumentsList");   

- 我创建了callService方法来调用webservice。:

public void callService(String operation, String soapaction)
    {
        try {
            String SOAP_ACTION = soapaction;
            String OPERATION_NAME = operation;
            String WSDL_TARGET_NAMESPACE =   getString(R.string.targetNamespace);
            String SOAP_ADDRESS = getString(R.string.soapAddress);
            SoapObject request = new SoapObject(
                    WSDL_TARGET_NAMESPACE, OPERATION_NAME);
             System.out.println("SOAP_ACTION "+SOAP_ACTION);
             System.out.println("OPERATION_NAME "+OPERATION_NAME);
             System.out.println("WSDL_TARGET_NAMESPACE "+WSDL_TARGET_NAMESPACE);
             System.out.println("SOAP_ADDRESS "+SOAP_ADDRESS);
            // System.out.println("SOAP_ACTION "+SOAP_ACTION);

            PropertyInfo propInfo = new PropertyInfo();

             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);

             HttpTransportSE httpTransport = new HttpTransportSE(
                    SOAP_ADDRESS);
envelope.setOutputSoapObject(request);

            httpTransport.call(SOAP_ACTION, envelope);

            SoapObject response = (SoapObject) envelope.bodyIn;


            if(response.getPropertyCount()>0){
                data=new StringBuilder();

            for (int i = 0; i < response.getPropertyCount(); i++) {

                Course c = new Course((SoapObject) response.getProperty(i));

                courseList.add(c);

            }

   for (Course c : courseList) {

                data.append("CourseName :" + c.getName());

                results.add(c.getName());
            }

- 如上所示,我将整个响应对象传递给另一个bean类来获取值。(课程c =新课程((SoapObject)response.getProperty(i));)

你能帮我解决一下如何解析这个响应对象以获取所需的String值。?

1 个答案:

答案 0 :(得分:0)

androidHttpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();

Vector<SoapObject> res= null;

if (response instanceof SoapObject) {
         res = new Vector();
     res.add((SoapObject) response);
     } else if (response instanceof Vector) {
             res = (Vector<SoapObject>) response;
         }

 for(SoapObject so1: res){
             //retrieve String here from soap object using this
                     so1.getProperty(0).toString(); 
    //then so1.getProperty(1).toString and likewise      
         }