Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse data from SOAP response in Android

Tags:

android

soap

I have the following soap output:

tabResponse
{
  tabResult=anyType
 {
   schema=anyType
  {
    element=anyType
     {complexType=anyType
       {
        choice=anyType
       {
         element=anyType
        {
        complexType=anyType
         {
         sequence=anyType
         {
         element=anyType{};
         element=anyType{};
           element=anyType{}; 
          element=anyType{};
              };
             }; 
           };
           };
              };
               };
              };
          diffgram=anyType
          {
          DocumentElement=anyType
         {sh=anyType
            {SchoolName=Bialik Hebrew Day School; SchoolType=Public elementary;  SchoolGrade=JK-12; City=Toronto; 
          };
          sh=anyType
          {SchoolName=Millwood Junior School; SchoolType=Public elementary;  SchoolGrade=JK-12; City=Toronto; 
           };
          sh=anyType
           {SchoolName=Dovercourt Junior Public School; SchoolType=Public elementary;  SchoolGrade=JK-12; City=Toronto;
           };
          sh=anyType{
           SchoolName=John English Junior Middle School; SchoolType=Public elementary;  SchoolGrade=JK-12; City=Toronto;
           };
         sh=anyType
         {SchoolName=Ossington Old Orchard Public School; SchoolType=Public elementary;  SchoolGrade=JK-12; City=Toronto; 
           };
           };
            };
            };
           }

And my code is like this:

SoapDemoActivity.java

public class SoapDemoActivity extends Activity {

private final String NAMESPACE = "****";
private final String URL = "*******";
private final String SOAP_ACTION_1 = "********";
private final String SOAP_ACTION_5 = "*******";
private final String METHOD_NAME_5 = "******";
Button mButtonTab;
TextView mTextViewResult;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
      mButtonTab=(Button)findViewById(R.id.button5);
      mButtonTab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            getDataTable(NAMESPACE,METHOD_NAME_5,SOAP_ACTION_5,URL);

        }
    });
 }
  public void getDataTable(String NAMESPACE, String METHOD_NAME,
        String SOAP_ACTION, String URL) {
    // TODO Auto-generated method stub
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    request.addProperty("uid","29");


    SoapSerializationEnvelope envelope = new      SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject response = (SoapObject)envelope.bodyIn;


        if(response != null)
        {

            SoapObject tabResponse = (SoapObject) response.getProperty(0);

            SoapObject tabResult = (SoapObject) tabResponse .getProperty(0);
            SoapObject schmea = (SoapObject) tabResult .getProperty(0);
            SoapObject elmemnt = (SoapObject) schmea .getProperty(0);
            SoapObject complextype = (SoapObject) elmemnt .getProperty(0);
            SoapObject choise = (SoapObject) complextype  .getProperty(0);
            SoapObject element_1 = (SoapObject) choise .getProperty(0);
            SoapObject complextype_1 = (SoapObject) element_1 .getProperty(0);
            SoapObject sequence = (SoapObject) complextype_1 .getProperty(0);
            SoapObject element_2 = (SoapObject) sequence .getProperty(0);
            SoapObject element_3 = (SoapObject) element_2 .getProperty(0);
            SoapObject element_4 = (SoapObject) element_3 .getProperty(0);
            SoapObject element_5 = (SoapObject) element_4 .getProperty(0);
            SoapObject diffgram = (SoapObject) element_5 .getProperty(0);
            SoapObject documentelement = (SoapObject) diffgram .getProperty(0);
            for(int i=0;i<documentelement.getPropertyCount();i++) 
            {
                SoapObject getAllData = (SoapObject) documentelement.getProperty(i);
                if (getAllData instanceof SoapObject) {
                    String data= getAllData.getProperty("sh").toString();
                    System.out.println("DATA "+data);

                }
            }

        }

        Object re= null;
        re = envelope.getResponse();
        Log.i("myApp", response.toString());

        System.out.println("dfdsfd "+re.toString());
        mTextViewResult.setText(re.toString());

    } catch (Exception e) {
        e.printStackTrace();
    }

}

 }

I get an error message like:

10-02 03:39:26.892: W/System.err(1284): java.lang.ArrayIndexOutOfBoundsException:   length=0; index=0
10-02 03:39:26.892: W/System.err(1284):     at  java.util.Vector.arrayIndexOutOfBoundsException(Vector.java:907)
10-02 03:39:26.892: W/System.err(1284):     at java.util.Vector.elementAt(Vector.java:328)
10-02 03:39:26.892: W/System.err(1284):     at org.ksoap2.serialization.SoapObject.getProperty(SoapObject.java:139)
10-02 03:39:26.892: W/System.err(1284):     at com.demosoap.SoapDemoActivity.getDataTable(SoapDemoActivity.java:241)
10-02 03:39:26.900: W/System.err(1284):     at com.demosoap.SoapDemoActivity$5.onClick(SoapDemoActivity.java:134)

This message give at line in my java file at

    SoapObject element_2 = (SoapObject) sequence .getProperty(0);

How can I parse this correctly?

like image 605
Harshal Kalavadiya Avatar asked Dec 25 '22 20:12

Harshal Kalavadiya


2 Answers

    SoapObject root = (SoapObject) response.getProperty(0);
    SoapObject s_deals = (SoapObject) root.getProperty("diffgram");
    SoapObject s_deals_1 = (SoapObject) s_deals.getProperty("DocumentElement");


    System.out.println("********Count : "+ s_deals_1.getPropertyCount());

    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;
             mStringSchoolName = category_list.getProperty("SchoolName").toString();
             mStringSchoolGrade = category_list.getProperty("SchoolGrade").toString();
             mStringSchoolType = category_list.getProperty("SchoolType").toString();
             mStringCity = category_list.getProperty("City").toString();

            System.out.println("mStringSchoolName "+mStringSchoolName);
            System.out.println("mStringSchoolGrade "+mStringSchoolGrade);
            System.out.println("mStringSchoolType"+mStringSchoolType);
            System.out.println("mStringCity "+mStringCity);
        }
    }
like image 109
Harshal Kalavadiya Avatar answered Dec 28 '22 11:12

Harshal Kalavadiya


You can use this code to parse Soap Object :

androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;


        if(resultsRequestSOAP != null)
        {

            SoapObject tabResponse = (SoapObject) resultsRequestSOAP.getProperty(0);

            SoapObject tabResult = (SoapObject) tabResponse .getProperty(0);

Like this , you can go through the hierarchy...

At the end, when you get element node :

            for(int i=0;i<elementData.getPropertyCount();i++) 
            {
                SoapObject getAllData = (SoapObject) elementData.getProperty(i);
                if (getAllData instanceof SoapObject) {
                    String data= getAllData.getProperty({property_name}).toString();

                }
            }

        }
        else
        {
              Log.i("No Response","error");
              return null;
        }
like image 38
SweetWisher ツ Avatar answered Dec 28 '22 12:12

SweetWisher ツ