4

I am trying to use a .NET webservice in my application where the service returns an array of objects as a response.

This is the format of the response from the web-service.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetPickersResponse xmlns="http://tempuri.org/">
  <GetPickersResult>
    <Picker>
      <Id>int</Id>
      <StartTime>dateTime</StartTime>
      <EndTime>dateTime</EndTime>
      <PickerCount>int</PickerCount>
    </Picker>
    <Picker>
      <Id>int</Id>
      <StartTime>dateTime</StartTime>
      <EndTime>dateTime</EndTime>
      <PickerCount>int</PickerCount>
    </Picker>
  </GetPickersResult>
</GetPickersResponse>
</soap:Body>
</soap:Envelope>

This is my Java code to get the response from the web-service.

SoapObject request = new SoapObject(NAMESPACE, METHOD_GET_CONTROL);
SoapSerializationEnvelope envelope = 
        new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);


HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

 try {
             androidHttpTransport.call(SOAP_ACTION_GET_CONTROL, envelope);
            ..........=envelope.getResponse(); //To get the data. }

My question, with what do I replace the "........" in my source code to receive the array of objects as a response from the service ? I need to receive multiple objects and then use their individual data members.

Please help. I am new to Web-services and Ksoap.

8
  • 1
    you solved your problem or not? Commented Jun 18, 2012 at 4:30
  • 1
    @SachinD : I did! Used a modified version of your code and it worked. Thanks a tonne. Commented Jun 18, 2012 at 5:48
  • can you please show me how you added more than one property in your webservice..ie..your Id,StartTime,EndTime,PickerCount..i just wanted to know how this can be achieved.. Commented Jan 20, 2015 at 15:48
  • @Lal : are you talking about the server end ? Commented Jan 20, 2015 at 21:11
  • yes..the server side..@Swayam Commented Jan 21, 2015 at 6:48

4 Answers 4

6

to get Multiple response just add few lines into your code instead of the ..........=envelope.getResponse();

SoapObject obj1 = (SoapObject) envelope.getResponse();

SoapObject obj2 =(SoapObject) obj1.getProperty(0);


for(int i=0; i<obj2.getPropertyCount(); i++)
{
   SoapObject obj3 =(SoapObject) obj2.getProperty(i);
   int id= Integer.parseInt(obj3.getProperty(0).toString());
   String start_date = obj3.getProperty(1).toString();
   String end_date = obj3.getProperty(2).toString();
   int id= Integer.parseInt(obj3.getProperty(3).toString());
}

If you still have any Problem you can write me.

Sign up to request clarification or add additional context in comments.

1 Comment

shouldn't it be: SoapObject obj3 =(SoapObject) obj2.getProperty(i); otherwise for each loop you pick position 0 from the second object instead of looping though its content
4

I always get Vector as response type, so my solution is:

        HttpTransportSE conn = new HttpTransportSE(url);
        try{
            conn.call(soapAction, envelope); //send request
            Vector<SoapObject> result= (Vector<SoapObject>)envelope.getResponse();

            int length = result.size();
            for(int i=0;i<length; ++i){
                SoapObject so = result.get(i);
                Log.d(TAG,so.getPropertyAsString(3));
            }
        } catch(Exception e){
            e.printStackTrace();
        }

1 Comment

I actually already solved it using a modified version of Sachin D's solution, but nevertheless, thank you for your help. And +1 for the use of Vector. Thanks! :)
0
package com.service;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class KSOAP extends Activity 
{
    ListView list;
    ArrayList<String> arraylist = new ArrayList<String>();
    ArrayAdapter<String>  arrayadapter;

    Button btn;
    TextView tv,tv2,tv3; 
    EditText no1,no2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
//tv3=(TextView)findViewById(R.id.textView3);

list=(ListView)findViewById(R.id.listView1);

no1=(EditText)findViewById(R.id.editText1);
no2=(EditText)findViewById(R.id.editText2);

btn =(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        String NAMESPACE = "http://tempuri.org/" ;    
        //"http://vladozver.org/";
        String METHOD_NAME = "GetSumOfTwoIntsList";//"GetStringList"; //
        String SOAP_ACTION = "http://tempuri.org/GetSumOfTwoIntsList";  
        //"http://vladozver.org/GetSumOfTwoInts";
        String URL = "http://192.168.0.203/recharge_service/service.asmx";
        //"http://192.168.0.203/Recharge_Service/Service.asmx";


        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo pi = new PropertyInfo();
        pi.setName("Operand1");
        pi.setValue(Integer.parseInt(no1.getText().toString()));
        pi.setType(int.class);
        Request.addProperty(pi);

        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("Operand2");
        pi2.setValue(Integer.parseInt(no2.getText().toString()));
        pi2.setType(int.class);
        Request.addProperty(pi2);


        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        try
        {
        AndroidHttpTransport transp = new AndroidHttpTransport(URL);
        transp.call(SOAP_ACTION, envelope);

        SoapObject obj1 = (SoapObject) envelope.bodyIn;

        SoapObject obj2 =(SoapObject) obj1.getProperty(0);


        for (int i = 0; i< obj2.getPropertyCount(); i++) 
            { 
                 int id1 = Integer.parseInt(obj2.getProperty(0).toString());
              // String id1=obj2.getProperty(0).toString();

               if(id1 != 0)
               { 
                 arraylist.add(""+ id1);    

               }
                /* tv3.setText(id3);*/
            }


        }
        catch(Exception e)
        { tv.setText(e.toString());
        e.printStackTrace();
        }

        //tv.setText(""+ count);



               list.setAdapter(arrayadapter);
    }
});

        arrayadapter = new ArrayAdapter<String>( this, 
        android.R.layout.simple_list_item_1, arraylist );
       list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }
    });

}


}

1 Comment

hear i am passing two parameter and getting list of no. as well this code can use for accessing and displaying String.
0

this is method

String NAMESPACE = "Your NameSpace" ;

//"http://vladozver.org/";

String METHOD_NAME = "get";//"GetStringList"; //

String SOAP_ACTION = "Your SoapAction";

//"http://vladozver.org/GetSumOfTwoInts";

String URL = "Your Soap Url";

public void involk(String na,String WebMethodName)throws IllegalStateException {

    SoapObject request=new SoapObject(NAMESPACE,WebMethodName);

    PropertyInfo sayHellopi=new PropertyInfo();

    sayHellopi.setName("get");
    sayHellopi.setValue(na);
    sayHellopi.setType(String.class);

    request.addProperty(sayHellopi);

    SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);

    AndroidHttpTransport transp = new AndroidHttpTransport(URL);

    try {
        transp.call(SOAP_ACTION,envelope);
        //ArrayList primitive= (ArrayList) envelope.bodyIn;
        SoapObject primitive= (SoapObject) envelope.bodyIn;
        // SoapObject obj2=(SoapObject)obj1.getProperty(0);
        // KvmSerializable ks=(KvmSerializable)envelope.bodyIn;
        // System.out.println("Values.............."+envelope.bodyIn);
               /* for(int i=0;i<obj2.getPropertyCount();i++){
                  String  id1=obj2.getProperty(0).toString();

                    if(""!=id1 ){

                        System.out.println("Count " + obj2.getPropertyCount());
                        arraylist.add(""+id1);
                        System.out.println("Array ::::::::::::: " + arraylist);

                    }
                }*/
                  //resText= envelope.bodyIn;

                   //resText = new ArrayList<Objects>();
           //for(int i=0;i<resText.equals(resText);i++){

          //}
                 System.out.println("List+++++++++++++++++");
               /* //System.out.println("value of result " + primitive);
                System.out.println("Count " + primitive.getPropertyCount());
                //System.out.println("Prop 1 " + primitive.getProperty(0));
                //System.out.println("A list b4 :; " + arrayList);*/

    /* for(int i=0;i<obj1.getPropertyCount();i++){
             //discount=new Discount((SoapObject)primitive.getProperty(i));
            //Toast.makeText(getApplicationContext(),"Hi",Toast.LENGTH_SHORT).show();

            //System.out.println("is result null????????????"+result);
            arraylist.add(obj1.getPropertyCount());
            System.out.println("Array ::::::::: " + arraylist);
        }*/
       // arraylist=new ArrayList();
        //resText=new String[primitive.getPropertyCount()];

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

            arraylist.add((String) primitive.getProperty(i));
            //arraylist.add(primitive.getPropertyCount());
            System.out.println("ForLoop--------------"+primitive.getProperty(i));


            // System.out.println("is result null????????????"+arrayList.listIterator());

        }
        // ListIterator it = arraylist.listIterator();

        //while(it.hasNext())
        // {
        // System.out.println("arrayListValue--------------"+it.next());
        //}
        // System.out.println("List+++++++++++++++++"+resText.length);

        // list.setAdapter(arrayadapter);

    }catch (Exception e){
        System.out.println("Error Value" + e);
        //resText="Error Occur";
        e.printStackTrace();
    }
    //list.setAdapter(arrayadapter);
  //return "";
}

using asynctask

private class AsyncCall extends AsyncTask {

    protected void onPreExecute(){

        super.onPreExecute();

        // pg.setVisibility(View.VISIBLE);
        dialog=new ProgressDialog(MainActivity.this);
        dialog.setIndeterminate(false);
        dialog.setMessage("Loding...");
        dialog.setCancelable(false);
        dialog.show();
    }
    protected Void doInBackground(Void... parms){
      involk(editText,"get");
       // return null;
       // return null;
        return null;
    }

    protected void onPostExecute(Void result){
        super.onPreExecute();
        // tv.setText(displayText);
        if(arraylist.size()!=0){
            dialog.dismiss();
        arrayadapter = new ArrayAdapter<String>( MainActivity.this,
                android.R.layout.simple_list_item_activated_1, arraylist );
            list.setAdapter(arrayadapter);
        }else{
            dialog.dismiss();
            getError();
        }
        // pg.setVisibility(View.INVISIBLE);
    }
}

Button Click event

btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            if(no1.getText().length()!=0&&no1.getText().length()!=0){

                editText=no1.getText().toString();
                AsyncCall task=new AsyncCall();
                task.execute();
            }
        }
    });

I Hope This is will help you friend :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.