如何从android中的Soap信封对象解析数据

时间:2015-07-22 05:31:04

标签: android soap

SoapObject result = (SoapObject) envelope.getResponse();
SoapObject root = (SoapObject) result.getProperty(0);
SoapObject s_deals = (SoapObject) root.getProperty("InfraWiseDetails");
SoapObject s_deals_1 = (SoapObject) s_deals.getProperty("VisitInfraDetails");

for (int i = 0; i < s_deals_1.getPropertyCount(); i++) {
    Object property = s_deals_1.getProperty(i);

    if (property instanceof SoapObject) {
        SoapObject category_list = (SoapObject) property;
        String x = category_list.getProperty("Feedback").toString();
        String y = category_list.getProperty("InfraName").toString();
        String z = category_list.getProperty("Problem").toString();
    }
}

这是我的代码

SoapObject SoapObject result = (SoapObject) envelope.getResponse();我得到了回应---&gt;

anyType{InfraWiseDetails=anyType{VisitInfraDetails=anyType{Feedback=Status OK; InfraName=Tables and Chairs; Problem=null; ResolutionStatus=false; Status=true; };

VisitInfraDetails=anyType{Feedback=Water Quality very poor; InfraName=Water; Problem=Rust in Water; ResolutionStatus=false; Status=false; }; }; VisitMasterId=1; }

我想从给定的SoapObject中解析数据并获取FeedBack,Infraname,Problem ...的所有值... 请告诉我哪里出错了我无法获得价值

2 个答案:

答案 0 :(得分:1)

经过很长一段时间我正在寻找ksoap2代码。试试这个:

SoapObject result = (SoapObject) envelope.getResponse();
SoapObject root = (SoapObject) result.getProperty(0);
SoapObject s_deals = (SoapObject) root.getProperty("InfraWiseDetails");

for (int i = 0; i < s_deals.getPropertyCount(); i++) {
    SoapObject s_deals_1 = (SoapObject) s_deals.getProperty(i);
    String x = s_deals_1.getProperty("Feedback").toString();
    String y = s_deals_1.getProperty("InfraName").toString();
    String z = s_deals_1.getProperty("Problem").toString();   
}

答案 1 :(得分:0)

在您的代码中尝试这样,您可以解析KSoap中的数据

getdata(String SearchValue) - 在do inBackground中调用此方法

public void getdata(String SearchValue) 
        {
            // Create request
            SoapObject request = new SoapObject(NAMESPACE2, METHOD_NAME2);



            PropertyInfo pi4 = new PropertyInfo();
            pi4.setName("City");
            pi4.setValue(SearchValue);// get the string that is to be sent to the webservice
            pi4.setType(String.class);
            request.addProperty(pi4);

            // Create envelope
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            // Set output SOAP object
            envelope.setOutputSoapObject(request);
            // Create HTTP call object
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL2);

            try {
                // Invole web service
                androidHttpTransport.call(SOAP_ACTION2, envelope);
                // Get the response
                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

                //Converting string to Array list
                  ArrayList<String> Servciecityname_arr= new ArrayList<String>();


                if ((response.toString()).contains("{")) 
                {

                    SoapObject rep = (SoapObject) envelope.bodyIn;
                    JSONArray jr = new JSONArray(rep.getPropertyAsString(0));
                    for (int i = 0; i < jr.length(); i++) {
                        JSONObject jb = (JSONObject) jr.get(i);


                           Cityname = jb.getString("CityName123");


                           Servciecityname_arr.add(Cityname);

                    }

                    CITYNAME = new String[Servciecityname_arr.size()];
                    CITYNAME = Servciecityname_arr.toArray(CITYNAME);


                } 
                else
                {
                    Status_Response = response.toString();
                }

            } catch (Exception e) {
                Log.i(TAG2, "Error in catch");
                e.printStackTrace();
            }

        }