1

I am working on a project with SOAP in Android Studio. I am new to SOAP and I have researched some articles. I am about to solve SOAP but I am stuck on something. I have a XML structure and I cannot get data from this XML tag. Actually I dont have any idea about the tags that start with "tem:...".

SOAP XML Tag

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:GetForexStocksandIndexesInfo>
         <tem:request>
            <tem:IsIPAD>true</tem:IsIPAD>
            <tem:DeviceID>test</tem:DeviceID>
            <tem:DeviceType>ipad</tem:DeviceType>
            <tem:RequestKey>%%UmVxdWVzdElzVmFsaWQxNjowNToyMDEyIDExOjU0%%</tem:RequestKey>
        <tem:Period>Day</tem:Period>
         </tem:request>
      </tem:GetForexStocksandIndexesInfo>
   </soapenv:Body>
</soapenv:Envelope>

And my Class to consume data

public void CallWebService() {
        final Date date = new Date();
        final String getCurrentDate = new SimpleDateFormat("dd:MM:yyyy HH:mm", Locale.ROOT).format(date);

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                key = encryptManager.PushData("RequestIsValid" + getCurrentDate);
                Log.i("Key: ", key);

                key2 = sharesandIndexesListManager.PushData2("true","test","ipad",key,"Day");
                Log.i("Key2: ", key2);
            }
        });

thread.start(); }

I see "key" var in log cause I see type of the variable in the web url but I dont see types of the variables, -for instance tem:DeviceType- in the web url. In addition Key2 returns null. Please help me:) Thanks.

And the error: org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:1 in java.io.InputStreamReader@21da6108)

nuhkoca
  • 1,777
  • 4
  • 20
  • 44

1 Answers1

0

I recommend using ksoap2-android library for parsing. If you don't know type of the value simply use getPropertyAsString on SoapObject you'll be getting.

Try this: SoapObject messageObject = (SoapObject) response.getProperty("request");

// Fetches last from above oject String lastAsString = messageObject.getPropertyAsString("DeviceID");

Look SoapObject methods here http://kobjects.org/ksoap2/doc/api/

Let me know if it helps.

UPD:

public class DeviceDetail implements KvmSerializable {

    public String IsIPAD = null;
    public String DeviceID= null;
   //TODO: ADD MORE VALUES YOURSELF! LIKE DeviceType

    private final String DATA_NAMESPACE = "http://schemas. YOUR SCHEMA HERE";

    public DeviceDetail () {
    }

    @Override
    public Object getProperty(int arg0) {

        switch (arg0) {
        case 0:
            return this.IsIPAD;
        case 1:
            return this.DeviceID;
        }

        return null;
    }

    @Override
    public int getPropertyCount() {

  //TODO: change to number of items accordingly
        return 2;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {

        arg2.namespace = DATA_NAMESPACE;

        switch (arg0) {
        case 0:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "IsIPAD";
            break;
        case 1:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "DeviceID";
            break;
        default:
            break;
        }

    }

    @Override
    public void setProperty(int arg0, Object arg1) {

        switch (arg0) {
        case 0:
            this.IsIPAD= arg1.toString();
            break;
        case 1:
            this.DeviceID= arg1.toString();
            break;
        default:
            break;
        }

    }

   }

First create request on server based on your needs:

//TODO: change to appropriate values
 String METHOD_NAME = "MyMethod";
 String INTERFACE = "IMyInterface";
 String NAMESPACE = "http://tempuri.org/";
 String SOAP_ACTION = NAMESPACE + INTERFACE + "/" + METHOD_NAME; 

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("APIKey", API_KEY);
request.addProperty("AuthToken",  AuthToken);
request.addProperty("UserID", 1);

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

Then you can execute call and parse your soap data:

  HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVER_URL); 
  androidHttpTransport.call(SOAP_ACTION, envelope);// call
  DeviceDetail responseObject = (DeviceDetail)envelope.bodyIn;
  //TODO: in responseObject now your data.
  String ssIPAD = responseObject.IsIPAD ;

Here you can find ksoap turorial with some explanation: http://seesharpgears.blogspot.com/2010/10/ksoap-android-web-service-tutorial-with.html

zkvarz
  • 611
  • 1
  • 8
  • 18
  • Firstly, thanks. I tries your code but your code doesn't return type, it returns value. I mean. I want to know that it is booelan, string or... And I think this is a nested soap and how can I access tem:deviceID or the others? – nuhkoca Aug 18 '16 at 08:26
  • @nuh-koca I've found an example to parse a complex response with ksoap2 library. Using KvmSerializable class we can work on nested object and access a name within a namespace: http://stackoverflow.com/questions/16602566/how-to-parse-complex-response-with-use-of-ksoap2-library-in-android If you don't know which type of value you're getting just specify as Strings and later on convert them using Long.getLong(value), for example; – zkvarz Aug 18 '16 at 09:12
  • Thanks bro I took a look at your post and I am now very confused ☺ Can you help me for my SOAP XML structure to how to build code classes? – nuhkoca Aug 18 '16 at 13:51
  • @nuh-koca I've updated my answer. I didn't use this ksoap myself but it's pretty much finished code here. – zkvarz Aug 18 '16 at 15:11
  • Thanks. I tried your code and in somewhere it returns "object reference not set to an instance of an object" error – nuhkoca Aug 18 '16 at 18:00
  • @nuhkoca I get the same response from web service. – Thracian Mar 09 '18 at 15:40