【问题标题】:The type of the expression must be an array type but it resolved to SoapObject表达式的类型必须是数组类型,但它解析为 SoapObject
【发布时间】:2012-12-12 04:38:28
【问题描述】:

我正在尝试将 aspx 网络服务返回的数组打印到 android TextView 上。我实际上可以使用以下 android 和 C# aspx 代码读取文本视图中的数据。但问题是,当我以以下格式打印输出时:

anyType{string="Shean";string="Ya";string="Hey";} 而不是 申雅&嘿

所以我决定读取数组如下:

for (int i = 0; i < intPropertyCount; i++) {
                SoapObject responseChild = (SoapObject) response.getProperty(i);
                int lengthOfResponseChild = responseChild.getPropertyCount();
                for (int j = 0; j < lengthOfResponseChild; j++) {
                    //Error Highlight is as responseChild[j]
                    result.setText(responseChild[j].toString());

                }
                //Used this line and prints output on a textView as anyType{string="Shean";string="Ya";string="Hey";}
                //result.setText(responseChild.toString());
            }

但是在

处突出显示了一个错误
result.setText(responseChild[j].toString());

声明The type of the expression must be an array type but it resolved to SoapObject

我的完整android代码如下:

package com.example.fp1_webservicedropdown;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Spinner;
import android.widget.TextView;
import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
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);

        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);
        AndroidHttpTransport aht = new AndroidHttpTransport(URL);

        try {

            aht.call(SOAP_ACTION, soapEnvelope);
            SoapObject response = (SoapObject) soapEnvelope.bodyIn;
            int intPropertyCount = response.getPropertyCount();

            for (int i = 0; i < intPropertyCount; i++) {
                SoapObject responseChild = (SoapObject) response.getProperty(i);
                int lengthOfResponseChild = responseChild.getPropertyCount();
                for (int j = 0; j < lengthOfResponseChild; j++) {
                    //Error Highlight is as responseChild[j]
                    result.setText(responseChild[j].toString());

                }
                //Used this line and prints output on a textView as anyType{string="Shean";string="Ya";string="Hey";}
                //result.setText(responseChild.toString());
            }


        } catch (Exception e) {

            e.printStackTrace();

        }
    }
}

我完整的asp web方法代码如下:

 [WebMethod]
    public string[] GetCustomerList()
    {
        //substitute code to actually populate the array with the dataset data
        string[] personIds = { "Shean", "Ya", "Hey" };
        return personIds;
    }

【问题讨论】:

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


    【解决方案1】:

    responseChild 是 SoapObject 的一个实例,但您正在尝试对其使用数组访问语法。

    如果您尝试遍历 responseChild 中的属性,SoapObject 有方法 getProperty(int index)

    使用它,您的循环将如下所示:

    for (int j = 0; j < lengthOfResponseChild; j++) {
      result.setText(responseChild.getProperty(j).toString());
    }
    

    【讨论】:

    • 用基本修复编辑了我的答案。请记住,我不熟悉 KSoap
    • ireddick 该死的救生员 :)) 非常感谢!我自己不会想出来的:)它起作用了
    猜你喜欢
    • 1970-01-01
    • 2021-07-24
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 2012-04-20
    • 2018-08-14
    相关资源
    最近更新 更多