【问题标题】:How to retrieve array ( list, vector , anything ) of objects as a result from .NET web service in Android?如何从 Android 中的 .NET Web 服务检索对象数组(列表、向量、任何内容)?
【发布时间】:2010-06-19 13:00:01
【问题描述】:
    public class a extends Activity {

    public static final String SOAP_ACTION = "http://vladozver.org/GetAllCategories";
    public static final String METHOD_NAME = "GetAllCategories";
    public static final String NAMESPACE = "http://vladozver.org/";
    public static final String URL = "http://192.168.1.3/Services/CategoryServices.asmx";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView)findViewById(R.id.TextView01);

        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);

        envelope.addMapping(NAMESPACE, "Category",new Category().getClass());

        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

        try
        {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject response = (SoapObject)envelope.getResponse();

            Object o = (Object)envelope.getResponse();
            SoapObject obj = (SoapObject)o;             
// DOESNT WORK ... 
            Category[] res = new Category[obj.getPropertyCount()];
            for (int i = 0; i < res.length; i++) {
                res[i] = (Category)obj.getProperty(i);
            }
// DOESNT WORK ...
                Vector<Category> cat = (Vector<Category>)response.getProperty(0);




        }
        catch(SoapFault sf)
        {

            sf.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}



public class Category implements KvmSerializable
{
    public int CategoryId;
    public String Name;
    public String Description;
    @Override
    public Object getProperty(int arg0) {

        switch(arg0)
        {
        case 0:
            return CategoryId;
        case 1:
            return Name;
        case 2:
            return Description;
        }

        return null;
    }
    @Override
    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 3;
    }
    @Override
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        // TODO Auto-generated method stub
        switch(index)
        {
        case 0:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "CategoryId";
            break;
        case 1:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "Name";
            break;
        case 2:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "Description";
            break;
        default:break;
        }
    }
    @Override
    public void setProperty(int index, Object value) {
        // TODO Auto-generated method stub
        switch(index)
        {
        case 0:
            CategoryId = Integer.parseInt(value.toString());
            break;
        case 1:
            Name = value.toString();
            break;
        case 2:
            Description = value.toString();
            break;
        default:
            break;
        }
    }
}

【问题讨论】:

    标签: .net android web-services


    【解决方案1】:

    你在使用 KSoap2 for android 吗?我不想告诉你这个,但肥皂是一些非常棘手的东西。您可能会遇到许多问题。我需要有关您的错误的更多信息,以便告诉您出了什么问题。当我这样做时,我必须实现我自己的 ServiceConnection/HttpTransport。我的回复以 xml 的形式出现,我必须将其解析为列表项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-05
      • 2022-09-23
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多