#StackBounty: #soap #android-ksoap2 Android SOAP Object reference set to an instance of an object

Bounty: 50

I need to use SOAP instead of REST. So far, i followed SOAP and kSOAP2 tutorials and threads here and youtube.

This is the web service for getting request key. I completed this without any errors. Time format: “dd:MM:yyyy HH:mm”

URL : http://mobileexam.veripark.com/mobileforeks/service.asmx

Request

<?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>
    <Encrypt xmlns="http://tempuri.org/">
      <request>RequestIsValid29:01:2015 16:31</request>
    </Encrypt>
  </soap:Body>
</soap:Envelope>

Response

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <EncryptResponse 
            xmlns="http://tempuri.org/">
         <EncryptResult>%%UmVxdWVzdElzVmFsaWQyOTowMToyMDE1IDE2OjMx%%</EncryptResult>
        </EncryptResponse>
    </soap:Body>
</soap:Envelope>

I only add necessary code to retrieve request key.

private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://mobileexam.veripark.com/mobileforeks/service.asmx";
private static final String METHOD_NAME_ENCRYPT = "Encrypt";
private static final String SOAP_ACTION_ENCRYPT = NAMESPACE + METHOD_NAME_ENCRYPT;

public void getRequestKey() {
    SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME_ENCRYPT);
    soapObject.addProperty("request", "RequestIsValid10:03:2018 16:30");
}

Next, using request key i need to get data but i can’t understand the format of web service and correct properties i guess.

<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>%%UmVxdWVzdElzVmFsaWQxNjowNToyMDEyIDExOjU4%%</tem:RequestKey>
            <tem:Period>Month</tem:Period>
         </tem:request>
      </tem:GetForexStocksandIndexesInfo>
   </soapenv:Body>
</soapenv:Envelope>

I can’t understand what does <tem:request> tag and other inner properties should be used to add properties to SoapObject in Java code.

    SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME_FOREX);
    soapObject.addProperty("IsIPAD", "true");
    soapObject.addProperty("DeviceID", "test");
    soapObject.addProperty("DeviceType", "ipad");
    soapObject.addProperty("RequestKey", requestKey);
    soapObject.addProperty("Period", "Month");

Returns Message=Object reference not set to an instance of an object

I also tried adding properties, using a KvmSerializable class adding property with PropertyInfo but nothing works.

public void getForexInfo() {

    SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME_FOREX);

    // TODO METHOD 1 Add Properties directly
    // soapObject.addProperty("IsIPAD", "true");
    // soapObject.addProperty("DeviceID", "test");
    // soapObject.addProperty("DeviceType", "ipad");
    // soapObject.addProperty("RequestKey", requestKey);
    // soapObject.addProperty("Period", "Day");

    // TODO METHOD 2 Use PropertyInfo
    // PropertyInfo pi = new PropertyInfo();
    // pi.setName("request");
    // pi.setValue(new SoapObject(NAMESPACE,
    // METHOD_NAME_FOREX).addProperty("IsIPAD", "true")
    // .addProperty("DeviceID", "test").addProperty("DeviceType", "ipad")
    // .addProperty("RequestKey", requestKey).addProperty("Period", "Day"));
    //
    // soapObject.addProperty(pi);

    // TODO METHOD 3 Create Custom Properties
    Properties properties = new Properties("true", "test", "ipad", requestKey, "Day");
    soapObject.addProperty("request", properties);

    // TODO METHOD 4 Add Object with Properties
    // SoapObject request = new SoapObject(NAMESPACE, "request");
    // request.addProperty("IsIPAD", "true");
    // request.addProperty("DeviceID", "test");
    // request.addProperty("DeviceType", "ipad");
    // request.addProperty("RequestKey", requestKey);
    // request.addProperty("Period", "Day");
    // soapObject.addSoapObject(request);

    // TODO METHOD 5 Add Request Directly
    // String requestString =
    // " <request>" +
    // " <IsIPAD>true</IsIPAD>" +
    // " <DeviceID>test</DeviceID>" +
    // " <DeviceType>ipad</DeviceType>" +
    // " <RequestKey>" + requestKey + "</RequestKey> " +
    // "<Period>Day</Period>" +
    // "</request>";
    // soapObject.addProperty(NAMESPACE, METHOD_NAME_FOREX, requestString);

    final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.dotNet = true;
    envelope.setOutputSoapObject(soapObject);
    // envelope.bodyOut = soapObject;

    // TODO Use with Method 3
    envelope.addMapping(NAMESPACE, "request", Properties.class);

    final HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
    httpTransportSE.debug = true;

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            tvResult.setText(httpTransportSE.requestDump);
        }
    });

    try {
        httpTransportSE.call(SOAP_ACTION_FOREX, envelope);
        final Object response = (Object) envelope.getResponse();
        System.out.println(httpTransportSE.requestDump);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvResult.setText(response.toString());
            }
        });
    } catch (final Exception e) {
        e.printStackTrace();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                tvResult.setText("EXCEPTION: " + e.getMessage());
            }
        });
    }
}

I would be grateful if you can provide a working example for this service.


Get this bounty!!!

Leave a Reply