简体   繁体   中英

Android: How to send object class to web service

I'm trying to send an Object Class to my ASP.NET Web Service using ksoap2-android.

SOAP request:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <sendTest xmlns="http://testing.com/">
            <test>
                <Id>int</Id>
                <Name>string</Name>
                <Age>int</Age>
            </test>
        </sendTest>
    </soap:Body>
</soap:Envelope>

C# class:

public class eTest
{
    int id;
    string name;
    int age;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public int Age
    {
        get { return age; }
        set { age = value; }
    }
}

Java class:

public class Test implements KvmSerializable {
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public Object getProperty(int index) {
        switch (index) {
            case 0:
                return getId();
            case 1:
                return getName();
            case 2:
                return getAge();
        }
        return null;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
        switch (index) {
            case 0:
                info.name = "Id";
                info.type = PropertyInfo.INTEGER_CLASS;
                break;
            case 1:
                info.name = "Name";
                info.type = PropertyInfo.STRING_CLASS;
                break;
            case 2:
                info.name = "Age";
                info.type = PropertyInfo.INTEGER_CLASS;
                break;
            default:
                break;
        }
    }

    @Override
    public int getPropertyCount() {
        return 3;
    }

    @Override
    public void setProperty(int index, Object value) {
        switch (index) {
            case 0:
                setId(Integer.parseInt(value.toString()));
                break;
            case 1:
                setName(value.toString());
                break;
            case 2:
                setAge(Integer.parseInt(value.toString()));
                break;
            default:
                break;
        }
    }
}

AsyncTask:

public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
    @Override
    protected Boolean doInBackground(Void... params) {
        String SOAP_ACTION = "http://testing.com/sendTest";
        String METHOD_NAME = "sendTest";
        String NAMESPACE = "http://testing.com/";
        String URL = "http://testing.com/WebService.asmx";

        Test test = new Test();
        test.setId(1);
        test.setName("Charly");
        test.setAge(26);

        PropertyInfo property = new PropertyInfo();
        property.setName("test");
        property.setType(Test.class);
        property.setValue(test);

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty(property);

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

        HttpTransportSE Transport = new HttpTransportSE(URL);
        try {
            Transport.call(SOAP_ACTION, envelope);
            return true;
        } catch (Exception e) {
            Log.e("ERROR", "KSOAP2", e);
        }
        return false;
    }
}

Where is the error? I don't get any exception in Logcat. sendTest method is an INSERT INTO Sql Query, but when I execute the AsyncTask, nothing happens.

问题解决了添加

envelope.addMapping(NAMESPACE, "eTest", Test.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM