1

嗨,我试图将从 aspx webservice 返回的项目数组传递给 android 中的微调器。但它返回null。我想知道我做错了什么。我之前确实尝试过返回一组字符串,并且它适用于相同的连接,任何帮助将不胜感激。

我的安卓代码如下:

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




    }
 }
4

1 回答 1

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

然后在再次将每个属性转换为 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
}

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

于 2012-12-11T07:53:57.093 回答