【问题标题】:Android: How to send object class to web serviceAndroid:如何将对象类发送到 Web 服务
【发布时间】:2016-05-07 15:40:27
【问题描述】:

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

SOAP 请求:

<?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# 类:

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 类:

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

异步任务:

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

错误在哪里?我在 Logcat 中没有任何异常。 sendTest 方法是一个 INSERT INTO Sql Query,但是当我执行 AsyncTask 时,什么也没有发生。

【问题讨论】:

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


    【解决方案1】:

    问题解决了添加

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

    【讨论】:

    • 如果我有这样的请求liochos.agsindia.com/HOSWEBSERVER/SendCustomerVehicleAddition"> intlongstring VNo> 字符串字符串字符串
    猜你喜欢
    • 2016-12-22
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多