HI im trying to pass array of items returned from aspx webservice to spinner in android. But it returns null. i would like to know what im doing wrong. I did try to return a set of strings before and it worked for the same connection, any help would be greatly appreciated.
My android code is as follows:
package com.example.fp1_webservicedropdown;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
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://mylocalip/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);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
.getResponse();
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();
}
}
}
and my web service code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
namespace HelloWorldNew
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://sample.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public ArrayList GetCustomerList()
{
ArrayList list = new ArrayList();
list.Add("Shean");
list.Add("Yo");
return list;
}
}
}
SoapPrimitive class definition:
A class that is used to encapsulate primitive types (represented by a string in XML serialization). Basically, the SoapPrimitive class encapsulates "unknown" primitive types (similar to SoapObject encapsulating unknown complex types). For example, new SoapPrimitive (classMap.xsd, "float", "12.3") ...
You are returning an ArrayList and it's not a Primitive type.
Instead of SoapPrimitive , try using SoapObject.
SoapObject response = (SoapObject) soapEnvelope.getResponse();
Then use response.getProperyCount()
to get number of items.
int intPropertyCount = response.getPropertyCount();
Then after casting each Property to SoapObject again, use toString() method. Like following:
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
}
I hope this solves your problem.