如何从Android中的.NET Web服务检索对象的数组(列表,向量,任何东西)?

时间:2010-06-19 13:00:02

标签: .net android web-services

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

1 个答案:

答案 0 :(得分:0)

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