1

I am communicating with a web-service by sending an Object along with the request.

This is the format of the WSDL of the web-service.

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

This is my java code to make the request.

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

}

I made my custom class KVMSerializable in the following manner.

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

    }
}

But, unfortunately, it is not working.

I am getting this error. I looked around a lot but still haven't been able to find a solution to this.

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

Please help. I am stuck on this same problem for more than 3 days now!

Swayam
  • 16,294
  • 14
  • 64
  • 102

3 Answers3

1

Why don't you use simple way.

final SoapObject request = new SoapObject(AppConsts.NAMESPACE,
                usecaseString);
        request.addProperty("Id", 1);
        request.addProperty("TabletId", 2);
        request.addProperty("SpeedVisibility", true);

so on till WebURL. and after that you can use this code.

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!!! ");

            }
Waqas
  • 4,411
  • 6
  • 27
  • 31
  • Sorry for my ignorance but what exactly is "usecaseString".. ? – Swayam Jul 05 '12 at 07:33
  • usecaseString is your method name of webservice which you want to call. in this case it is "UpdateLocation". – Waqas Jul 05 '12 at 07:34
  • Well, in that case, it would not work. I have tried it before too. But if you see my WSDL, you would understand that I am not passing multiple values. I am passing a single object, not multiple primitive values. – Swayam Jul 05 '12 at 07:42
  • Can you provide what values are you using of these variables NAMESPACE, URL, and SOAP_ACTION? and your service is hosted on localhost right? – Waqas Jul 05 '12 at 09:43
  • But what about the nested object, int – Md Imran Choudhury Mar 07 '19 at 06:35
0

Oh I see.. this is the tutorial which will tell you how to do it.

Waqas
  • 4,411
  • 6
  • 27
  • 31
  • If you see the code in my question then you would see that I have followed all the steps mentioned there.. – Swayam Jul 05 '12 at 10:06
  • Can you provide what values are you using of these variables NAMESPACE, URL, and SOAP_ACTION? and your service is hosted on localhost right? – Waqas Jul 05 '12 at 10:14
  • No no. My service is not on a local host. This is the url to my service. http://83.83.166.133:82/WebService_Mush.asmx?op=UpdateLocation – Swayam Jul 05 '12 at 10:40
  • Other declarations, private static final String SOAP_ACTION_UPDATE_SETTINGS = "http://tempuri.org/UpdateLocation"; private static final String METHOD_UPDATE_SETTINGS = "UpdateLocation"; private static final String NAMESPACE = "http://tempuri.org/"; private static String URL = "http://83.83.166.133:82/WebService_Mush.asmx"; – Swayam Jul 05 '12 at 10:41
  • I hope issue isn't about missing "http://" in urls. everything else looks fine to me. you can check your request by using request dump. see if you are sending request in valid format. I hope you know how to see the request dump. – Waqas Jul 05 '12 at 10:58
  • No, my code does have the "http://" for the NAMESPACE and URL. Maybe stackoverflow is removing the http:// syntax and making it a hyperlink. Anyway, can you tell me how to check the request dump ? I have never done that. Thanks in advance. – Swayam Jul 05 '12 at 11:12
  • 1
    `AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL); androidHttpTransport.debug = true; androidHttpTransport.call(SOAP_ACTION_UPDATE_SETTINGS, envelope);` `Log.d("MyAPP", "----------------- " + androidHttpTransport.requestDump );`. will be thankful if you upvote my comment :). thanks. – Waqas Jul 05 '12 at 11:36
  • Yeah thanks a lot man. But, what do I have to write in place of your "---------" ? – Swayam Jul 05 '12 at 12:14
  • oh... It's just a string you can use anything like "request dump = " or even nothing :). – Waqas Jul 05 '12 at 12:16
  • 1
    `Log.d("MyAPP", androidHttpTransport.requestDump);` This will aslo work. – Waqas Jul 05 '12 at 12:17
  • 1
    you got it working or still need it to work? I think I will take some time and will try to get this work personally, then will share code with you. If you still need it. – Waqas Jul 10 '12 at 04:34
  • Well, no. I tried everything that you suggested but I am not being to pass an Object as a whole. So, I changed my web-service to accept individual data members. But I would indeed be grateful to you if you could help me out with your personal code. – Swayam Jul 10 '12 at 07:43
  • this is the URL of my web-service http://83.83.166.133:82/WebService_Mush.asmx?op=UpdateLocation – Swayam Jul 10 '12 at 07:47
0

What about handling that Object on the server side, when the server application gets your Object how does the server source code handle that Object?

E_X
  • 3,722
  • 1
  • 14
  • 15