试图将从webservice返回的数组项传递给android中的spinner。但它返回null

时间:2012-12-11 06:32:33

标签: java android asp.net web-services android-ksoap2

我想尝试将从aspx webservice返回的项目数组传递给android中的spinner。但它返回null。我想知道我做错了什么。我之前尝试返回一组字符串并且它适用于同一个连接,任何帮助都将非常感激。

我的Android代码如下:

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

        }
    }
}

我的网络服务代码如下:

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




    }
 }

1 个答案:

答案 0 :(得分:1)

SoapPrimitive类定义:

  

用于封装基本类型的类(由XML序列化中的字符串表示)。基本上,SoapPrimitive类封装了“未知”原始类型(类似于封装未知复杂类型的SoapObject)。例如,新的SoapPrimitive(classMap.xsd,“float”,“12.3”)......

您正在返回一个ArrayList,它不是Primitive类型。

尝试使用SoapObject。而不是SoapPrimitive。

SoapObject response = (SoapObject) soapEnvelope.getResponse();

然后使用response.getProperyCount()获取项目数。

 int intPropertyCount = response.getPropertyCount();

然后再次将每个Property转换为SoapObject后,使用toString()方法。如下:

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
}

我希望这能解决你的问题。