KSoap2 Android accepts an array of objects

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

This is a web service response format.

<?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 a response from a 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 is, what will I replace "........" in my source code to get an array of objects as a response from the service? I need to get multiple objects and then use their individual data items.

Please, help. I am new to web services and Ksoap.

+4
source share
4 answers

to get a multiple answer, just add a few lines to your code instead ..........=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 a problem, you can write to me.

+6
source

I always get Vector as the type of response, 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(); } 
+3
source
 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 } }); } } 
0
source

this is a 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 inck (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

AsyncCall private class 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); } } 

Click Event Button

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(); } } }); 

Hope this helps you friend :)

0
source

All Articles