Search code examples
c#androidweb-servicesksoap2

array returned from a web service to an android text view prints anyTpe{String instead of the values only


I want to print the array returned from a web service to an android text view. But when it prints the array it displays the results as "anyType{string=Shean;string=Ya;string=Hey;} instead of printing it as Shean Ya Hey.. any idea what im doing wrong?

The android code is as follows:

package com.example.fp1_webservicedropdown;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.Spinner;
    import android.widget.TextView;
    import org.ksoap2.*;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.*;

    public class MainActivity extends Activity {
        TextView result;
        Spinner spinnerC;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            spinnerC = (Spinner) findViewById(R.id.spinner1);
            result = (TextView) findViewById(R.id.textView2);

            final String NAMESPACE = "http://sample.com/";
            final String METHOD_NAME = "GetCustomerList";
            final String SOAP_ACTION = "http://sample.com/GetCustomerList";
            final String URL = "http://10.113.28.241/HelloWorldNew/Service1.asmx";

            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            // Request.addProperty("a", "32");
            // Request.addProperty("b", "12");

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);
            AndroidHttpTransport aht = new AndroidHttpTransport(URL);

            try {
                aht.call(SOAP_ACTION, soapEnvelope);
                SoapObject response = (SoapObject) soapEnvelope.bodyIn;
                int intPropertyCount = response.getPropertyCount();

                for (int i = 0; i < intPropertyCount; i++) {
                    SoapObject responseChild = (SoapObject) response.getProperty(i);
                    result.setText(responseChild.toString());
                    // You can add all strings in a list and give
                    // ArrayAdapter<String> for Spinner
                }

                /*
                 * result.setText("The web service returned " +
                 * resultString.toString());
                 */
                // ---------- result To Spinner Code -----------------------//
                /*
                 * String[] toSpinner = new String[] { resultString.toString() };
                 * ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 * android.R.layout.simple_spinner_item, toSpinner);
                 * adapter.setDropDownViewResource
                 * (android.R.layout.simple_spinner_dropdown_item);
                 * spinnerC.setAdapter(adapter);
                 */
                // ---------- result To Spinner Code ends here -----------------//

            } catch (Exception e) {

                e.printStackTrace();

            }
        }
    }

The web method is as follows:

 [WebMethod]
        public string[] GetCustomerList()
        {
            //substitute code to actually populate the array with the dataset data
            string[] personIds = { "Shean", "Ya", "Hey" };
            return personIds;
        }

Solution

  • Does this work?

    for (int i = 0; i < intPropertyCount; i++) {
        SoapObject responseChild = (SoapObject) response.getProperty(i);
        for (int j = 0; j < lengthOfResponseChild; j++)
            result[j].setText(responseChild[j].toString());
        }
    }
    

    This is just rough code. You may need to clean up to get the right functions and variables.