Android Ksoap2将复杂对象作为参数传递

Android Ksoap2将复杂对象作为参数传递,android,android-ksoap2,custom-object,Android,Android Ksoap2,Custom Object,我通过发送一个对象和请求来与web服务通信 private void updateSettingsOnServer() { SoapObject request = new SoapObject(NAMESPACE, METHOD_UPDATE_SETTINGS); Location serverObject = new Location(currentLocation.databaseId, currentLocation.tabletId, currentLocation.

我通过发送一个对象和请求来与web服务通信

private void updateSettingsOnServer() {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_UPDATE_SETTINGS);

    Location serverObject = new Location(currentLocation.databaseId, currentLocation.tabletId, currentLocation.isSpeedVisible, currentLocation.isBeltVisible, currentLocation.isLightVisible, currentLocation.isProjectorVisible, currentLocation.slotStarting, currentLocation.slotEnding, prefPickerCount, prefSlotCount, prefServerIPString, prefWebURL);


     PropertyInfo pi = new PropertyInfo();
        pi.setName("serverObject");
        pi.setValue(serverObject);
        pi.setType(serverObject.getClass());
        request.addProperty(pi);




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



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


    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);




    try {
        androidHttpTransport.call(SOAP_ACTION_UPDATE_SETTINGS, envelope);




        SoapObject result=(SoapObject)envelope.getResponse(); //To get the data.
        tempText.setText("Received :" + result.toString());

    }
    catch(Exception e)
    {

        tempText.setText("Error");
        //Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

}
这是web服务的WSDL格式

<?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>
<UpdateLocation xmlns="http://tempuri.org/">
  <location>
    <Id>int</Id>
    <TabletId>int</TabletId>
    <SpeedVisibility>boolean</SpeedVisibility>
    <BeltVisibility>boolean</BeltVisibility>
    <LightVisibility>boolean</LightVisibility>
    <ProjectorVisibility>boolean</ProjectorVisibility>
    <DefStart>int</DefStart>
    <DefEnd>int</DefEnd>
    <DefPickerCount>int</DefPickerCount>
    <DefSlotCount>int</DefSlotCount>
    <ServerIP>string</ServerIP>
    <WebURL>string</WebURL>
  </location>
</UpdateLocation>
</soap:Body>
</soap:Envelope>
我用以下方式使我的定制类KVM可变

class Location implements KvmSerializable
{
    public Location(int id, int tabletid, boolean speed, boolean belt, boolean light, boolean projector, int start, int end, int picker, int slot, String server, String web)
    {
        databaseId =id;
        tabletId = tabletid;
        isSpeedVisible = speed;
        isBeltVisible = belt;
        isLightVisible = light;
        isProjectorVisible = projector;
        slotStarting = start;
        slotEnding = end;
        pickerCounting = picker;
        slotCounting = slot;
        serverUrlLink = server;
        webUrlLink = web;


    }

    public Location() {
        // TODO Auto-generated constructor stub
    }

    boolean isSpeedVisible, isLightVisible, isBeltVisible, isProjectorVisible;
    int slotStarting, slotEnding , pickerCounting, slotCounting;
    int databaseId, tabletId;
    String serverUrlLink, webUrlLink;


    public Object getProperty(int arg0) {
        // TODO Auto-generated method stub

         switch(arg0)
            {
            case 0:
                return databaseId;
            case 1:
                return tabletId;
            case 2:
                return isSpeedVisible;
            case 3:
                return isBeltVisible;
            case 4:
                return isLightVisible;
            case 5:
                return isProjectorVisible;
            case 6:
                return slotStarting;
            case 7:
                return slotEnding;
            case 8:
                return pickerCounting;
            case 9:
                return slotCounting;
            case 11:
                return serverUrlLink;
            case 12:
                return webUrlLink;

            }

         return null;
    }


    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 12;
    }



    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo info) {
        // TODO Auto-generated method stub

        switch(arg0)
        {
        case 0:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "databaseId";
            break;
        case 1:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "tabletId";
            break;
        case 2:
            info.type = PropertyInfo.BOOLEAN_CLASS;
            info.name = "isSpeedVisible";
            break;
        case 3:
            info.type = PropertyInfo.BOOLEAN_CLASS;
            info.name = "isBeltVisible";
            break;
        case 4:
            info.type = PropertyInfo.BOOLEAN_CLASS;
            info.name = "isLightVisible";
            break;
        case 5:
            info.type = PropertyInfo.BOOLEAN_CLASS;
            info.name = "isProjectorVisible";
            break;
        case 6:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "slotStarting";
            break;
        case 7:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "slotEnding";
            break;
        case 8:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "pickerCounting";
            break;
        case 9:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "slotCounting";
            break;
        case 11:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "serverUrlLink";
            break;
        case 12:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "webUrlLink";
            break;

        default:break;
        }



    }
    public void setProperty(int index, Object value) {
        // TODO Auto-generated method stub

        switch(index)
        {
        case 0:
            databaseId = Integer.parseInt(value.toString());
            break;
        case 1:
            tabletId = Integer.parseInt(value.toString());
            break;
        case 2:
            isSpeedVisible = Boolean.parseBoolean(value.toString());
            break;
        case 3:
            isBeltVisible = Boolean.parseBoolean(value.toString());
            break;
        case 4:
            isLightVisible = Boolean.parseBoolean(value.toString());
            break;
        case 5:
            isProjectorVisible = Boolean.parseBoolean(value.toString());
            break;
        case 6:
            slotStarting = Integer.parseInt(value.toString());
            break;
        case 7:
            slotEnding = Integer.parseInt(value.toString());
            break;
        case 8:
            pickerCounting = Integer.parseInt(value.toString());
            break;
        case 9:
            slotCounting = Integer.parseInt(value.toString());
            break;
        case 10:
            serverUrlLink = (value.toString());
            break;
        case 11:
            webUrlLink = (value.toString());
            break;


        default:
            break;
        }

    }
}
但是,不幸的是,它不起作用

我收到了这个错误。我环顾了很多地方,但仍然没有找到解决这个问题的办法

SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Object reference not set to an instance of an object.' faultactor: 'null' detail: org.kxml2.kdom.Node@410cdac8

请帮忙。我被困在这个问题上超过3天了

为什么不使用简单的方法呢

final SoapObject request = new SoapObject(AppConsts.NAMESPACE,
                usecaseString);
        request.addProperty("Id", 1);
        request.addProperty("TabletId", 2);
        request.addProperty("SpeedVisibility", true);
直到WebURL。然后你可以使用这个代码

final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        final HttpTransportSE androidHttpTransport = new HttpTransportSE(
                AppConsts.URL);
        androidHttpTransport.debug = true;
        String soapAction = AppConsts.NAMESPACE + usecaseString;

        try {
            androidHttpTransport.call(soapAction, envelope);
            SoapPrimitive resultSoapPrimitive;
            resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
            if (resultSoapPrimitive != null) {
                result = resultSoapPrimitive.toString();
                if (AppConsts.ENABLE_LOG)
                    Log.d(AppConsts.GAME_TITLE, "result json : " + result);
            } else {
                if (AppConsts.ENABLE_LOG)
                    Log.d(AppConsts.GAME_TITLE, "result json is NULL!!! ");

            }

你为什么不用简单的方法呢

final SoapObject request = new SoapObject(AppConsts.NAMESPACE,
                usecaseString);
        request.addProperty("Id", 1);
        request.addProperty("TabletId", 2);
        request.addProperty("SpeedVisibility", true);
直到WebURL。然后你可以使用这个代码

final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        final HttpTransportSE androidHttpTransport = new HttpTransportSE(
                AppConsts.URL);
        androidHttpTransport.debug = true;
        String soapAction = AppConsts.NAMESPACE + usecaseString;

        try {
            androidHttpTransport.call(soapAction, envelope);
            SoapPrimitive resultSoapPrimitive;
            resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
            if (resultSoapPrimitive != null) {
                result = resultSoapPrimitive.toString();
                if (AppConsts.ENABLE_LOG)
                    Log.d(AppConsts.GAME_TITLE, "result json : " + result);
            } else {
                if (AppConsts.ENABLE_LOG)
                    Log.d(AppConsts.GAME_TITLE, "result json is NULL!!! ");

            }


哦,我明白了。。这是教你如何做的教程。

哦,我明白了。。是将告诉您如何操作的教程。

如何在服务器端处理该对象,当服务器应用程序获取您的对象时,服务器源代码如何处理该对象?

如何在服务器端处理该对象,当服务器应用程序获取您的对象时,服务器源代码如何处理该对象?

对不起,我不知道“usecaseString”到底是什么?usecaseString是要调用的webservice的方法名。在这种情况下,它是“UpdateLocation”。在这种情况下,它将不起作用。我以前也试过。但如果您看到我的WSDL,您就会明白我并没有传递多个值。我只传递一个对象,而不是多个原语值。您能提供这些变量名称空间、URL和SOAP_操作的值吗?您的服务托管在localhost上,对吗?但是嵌套对象呢,对不起,我不知道“usecaseString”到底是什么?usecaseString是要调用的webservice的方法名。在这种情况下,它是“UpdateLocation”。在这种情况下,它将不起作用。我以前也试过。但如果您看到我的WSDL,您就会明白我并没有传递多个值。我只传递一个对象,而不是多个原语值。您能提供这些变量名称空间、URL和SOAP_操作的值吗?您的服务托管在localhost上,对吗?但是嵌套对象int呢?如果您看到我问题中的代码,那么您会看到我已经遵循了这里提到的所有步骤。您可以提供这些变量名称空间、URL和SOAP\u操作的值吗?并且您的服务托管在本地主机上,对吗?
AndroidHttpTransport AndroidHttpTransport=newandroidhttptransport(URL);androidHttpTransport.debug=true;调用(SOAP\u操作\u更新\u设置,信封)
Log.d(“MyAPP”、“------------------------”+androidHttpTransport.requestDump)。如果您支持我的评论,我将不胜感激:)。谢谢。
Log.d(“MyAPP”,androidHttpTransport.requestDump)这也可以。你让它工作了还是仍然需要它工作?我想我会花一些时间,并将试图得到这项工作的个人,然后将与您共享代码。如果您仍然需要它。如果您看到我问题中的代码,那么您将看到我已经遵循了这里提到的所有步骤。您可以提供这些变量名称空间、URL和SOAP_操作的值吗?并且您的服务托管在本地主机上,对吗?
AndroidHttpTransport AndroidHttpTransport=newandroidhttptransport(URL);androidHttpTransport.debug=true;调用(SOAP\u操作\u更新\u设置,信封)
Log.d(“MyAPP”、“------------------------”+androidHttpTransport.requestDump)。如果您支持我的评论,我将不胜感激:)。谢谢。
Log.d(“MyAPP”,androidHttpTransport.requestDump)这也可以。你让它工作了还是仍然需要它工作?我想我会花一些时间,并将试图得到这项工作的个人,然后将与您共享代码。如果你还需要它。@user:1063062:你能帮我吗?@user:1063062:你能帮我吗?你好!谢谢你花时间回答,但是问题已经解决了。如果问题已经解决了,你能把答案贴出来吗?非常感谢。你好谢谢你花时间回答,但是问题已经解决了。如果问题已经解决了,你能把答案贴出来吗?非常感谢。