Android:如何将对象类发送到Web服务

时间:2016-01-29 19:21:04

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

我正在尝试使用kso​​ap2-android将对象类发送到我的ASP.NET Web服务。

SOAP请求:

map[string]interface{}

C#class:

<?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>

Java类:

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

的AsyncTask:

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

错误在哪里?我在Logcat中没有任何异常。 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; } } 方法是一个INSERT INTO Sql Query,但是当我执行AsyncTask时,没有任何反应。

1 个答案:

答案 0 :(得分:0)

问题解决了添加

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