Returning Array of Primitive Types with KSOAP



KSOAP until recently had an issue with returning an array of primitive types. With the new patch the issue is solved. If you haven't downloaded the newest version of
KSOAP available on GitHub, you can use the following snippet:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope =
            new SoapSerializationEnvelope(SoapEnvelope.VER11);
            //envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);

                KvmSerializable ks = (KvmSerializable)envelope.bodyIn;
                for(int i=0;i<ks.getPropertyCount();i++)
                {
                   ks.getProperty(i); //if complex type is present then you can cast this to SoapObject and if primitive type is returned you can use toString() to get actuall value.
                }



This code was written by nebe.daredevil

If anyone else is having trouble to return array of primitive types with KSOAP, might consider the issue thread on Google Code

I hope the agony with KSOAP is near an end with this.

8 comments:

Valter said...

Your blog is helping me a lot. Took me out of a hole.

Tanks

SeeSharpWriter said...

I am so glad I helped someone.

Anonymous said...

Thanks great blog you have help me lots, only one thing how to feed ks.getProperty(i); right into the SQLite on the device?

SeeSharpWriter said...

Perhaps you should write some code to insert the retrieved property into the SQLite database. I believe you should refer to some tutorial on working with SQLite and then combine the codes.

Rajan Thakrar said...

Hello,
i am trying to call a web service (SOAP - .asmx web service [.net]) since last 2 to 3 days. I get the response but my problem is the response is neither JSON nor XML, so i didn't understand how can i parse my response string?

here i am putting my response string
----------------------------------------------
anyType{NewDataSet=anyType{


Table=anyType{AdSpaceID=423; AdspaceName=Hoarding at football, (Amul); Address=football,(Amul)-Ahmedabad; };

Table=anyType{AdSpaceID=424; AdspaceName=Hoarding at football, tow. Bhulabhai.(Amul); Address=football, tow. Bhulabhai.(Amul)-Ahmedabad; };

Table=anyType{AdSpaceID=425; AdspaceName=Hoarding at Bhulabhai 4 Road Junction; Address=Bhulabhai 4 Road Junction-Ahmedabad; };

Table=anyType{AdSpaceID=426; AdspaceName=Hoarding at Bhulabhai Park, Tow. S.T. Stand; Address=Bhulabhai Park, Tow. S.T. Stand-Ahmedabad; }; }; }
----------------------------------------------

above string is my response string and i want to
convert it into xml, so i can parse it...

Help me

Than you

Anonymous said...

hi seesharpwriter,
i followed your tutorial and it helped me a lot. thanks for that.
i am having an issue, i cannot access the array returned from my web service in android, i can access an individual data but not the array.
I am getting xmlpullparserexception expected Start tag error.

public class MainActivity extends Activity {

private final String NAMESPACE = "urn:hellowsdl";
private final String URL = "http://10.0.2.2/WebServiceServer/test.php?wsdl"; // WSDL URL
private final String SOAP_ACTION = "urn:hellowsdl#hello";
private final String METHOD_NAME = "hello"; // Method on web service

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}

Button btnSend = (Button) this.findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/*menu M=new menu();

PropertyInfo pi = new PropertyInfo();
M.Menu_id=1;
pi.setName("M");
pi.setValue(M);
pi.setType(M.getClass());*/


TextView txtResult = (TextView) findViewById(R.id.txtResult);

EditText txtName = (EditText) findViewById(R.id.txtName);


SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//request.addProperty(pi);
request.addProperty("strName", txtName.getText().toString());

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);
//envelope.addMapping(NAMESPACE, "menu",new menu().getClass());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//androidHttpTransport.debug=true;


try {

androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.getResponse();
System.out.println(result);
if (result != null) {

KvmSerializable ks = (menu)(KvmSerializable)envelope.bodyIn;
for(int i=0;i<ks.getPropertyCount();i++)
{
txtResult.setText(ks.getProperty(i).toString());


}} else {
Toast.makeText(getApplicationContext(),
"Web Service not Response!", Toast.LENGTH_LONG)
.show();


}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


could you please help me with that?
thanks ;)


Anonymous said...

I need to send some value data(i receive this data from one activity) to a webservice and receive 10elements, but the .call have an exeption

Geet choudhary said...

Hoarding advertising provides a boundless range of improvement as compared to other advertising mediums. The brand message conveyed through OOH media is also clutter-free.
Hoardings Agency across India

Post a Comment