package com.example.sameer.soaptest3;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Node;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
    SoapObject resultsRequestSOAP = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TextView textView = (TextView) findViewById(R.id.tv);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AsynW asynW = new AsynW();
        asynW.execute();
    }
    class AsynW extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... voids) {
            getCurrentLocations();
            return null;
        }
    }
    private void getCurrentLocations() {
        String NAMESPACE = "http://tempuri.org/";//"https://api.authorize.net/soap/v1/";//
        String URL ="http://www.motortrackers.com//motortrackersservice.asmx";//"https://apitest.authorize.net/soap/v1/Service.asmx?wsdl"; //
        String SOAP_ACTION = "http://tempuri.org/GetVehicleHistory";//"https://api.authorize.net/soap/v1/AuthenticateTest";//
        String METHOD_NAME = "GetVehicleHistory";
        org.kxml2.kdom.Element header[] = new org.kxml2.kdom.Element[1];
        header[0] = new org.kxml2.kdom.Element().createElement(NAMESPACE, "AuthHeader");
        org.kxml2.kdom.Element Username = new org.kxml2.kdom.Element().createElement(NAMESPACE, "Username");
        Username.addChild(Node.TEXT, "test");
        header[0].addChild(Node.ELEMENT, Username);
        org.kxml2.kdom.Element Password = new org.kxml2.kdom.Element().createElement(NAMESPACE, "Password");
        Password.addChild(Node.TEXT, "test");
        header[0].addChild(Node.ELEMENT, Password);
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        PropertyInfo pVehicleId = new PropertyInfo();
        pVehicleId.setName("VehicleId");
        pVehicleId.setValue(2);
        pVehicleId.setType("VehicleId".getClass());
        request.addProperty(pVehicleId);
         //request.addProperty("VehicleId",2);
        PropertyInfo pStartDate = new PropertyInfo();
        pStartDate.setName("StartDate");
        pStartDate.setValue("09/10/2017 05:20");
        pStartDate.setType("StartDate".getClass());
        request.addProperty(pStartDate);
       // request.addProperty("StartDate","10/5/2017 05:20");
        PropertyInfo pEndDate = new PropertyInfo();
        pEndDate.setName("EndDate");
        pEndDate.setValue("10/10/2017 05:20");
        pEndDate.setType("EndDate".getClass());
        request.addProperty(pEndDate);
        //request.addProperty("EndDate","10/6/2017 05:20");
        PropertyInfo pInterval = new PropertyInfo();
        pInterval.setName("Interval");
        pInterval.setValue(1);
        pInterval.setType("Interval".getClass());
        request.addProperty(pInterval);
        //request.addProperty("Interval",1);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.headerOut = header;
        envelope.implicitTypes = false;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        System.out.println("Request: "+request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            resultsRequestSOAP = (SoapObject) envelope.bodyIn;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        System.out.println("resultsRequestSOAP: " + resultsRequestSOAP);
        SoapObject root = (SoapObject) resultsRequestSOAP.getProperty(0);
        System.out.println("root:   " + root);
        for (int i = 0; i < root.getPropertyCount(); i++) {
            Object property = root.getProperty(i);
            if (property instanceof SoapObject) {
                SoapObject category_list = (SoapObject) property;
                SetCurrentLocationInfo setCurrentLocationInfo = new SetCurrentLocationInfo();
                setCurrentLocationInfo.IMEI = category_list.getProperty("IMEI").toString();
                setCurrentLocationInfo.VehicleId = category_list.getProperty("VehicleId").toString();
                setCurrentLocationInfo.DateTime = category_list.getProperty("DateTime").toString();
                setCurrentLocationInfo.GpsSateliteCount = category_list.getProperty("GpsSateliteCount").toString();
                setCurrentLocationInfo.Latitude = category_list.getProperty("Latitude").toString();
                setCurrentLocationInfo.Longitude = category_list.getProperty("Longitude").toString();
                setCurrentLocationInfo.Speed = category_list.getProperty("Speed").toString();
                setCurrentLocationInfo.IgnitionStatus = category_list.getProperty("IgnitionStatus").toString();
                setCurrentLocationInfo.VehicleNo = category_list.getProperty("VehicleNo").toString();
                setCurrentLocationInfo.PacketReceivedAt = category_list.getProperty("PacketReceivedAt").toString();
            }
        }
    }
display :- root:   anyType{}
webservice :-
<soap:Body>
    <GetVehicleHistoryResponse xmlns="http://tempuri.org/">
      <GetVehicleHistoryResult>
        <LocationPacket>
          <IMEI>string</IMEI>
          <VehicleId>double</VehicleId>
          <DateTime>dateTime</DateTime>
          <GpsSateliteCount>string</GpsSateliteCount>
          <Latitude>string</Latitude>
          <Longitude>string</Longitude>
          <Speed>string</Speed>
          <IgnitionStatus>string</IgnitionStatus>
          <GasOilElectricityConnected>string</GasOilElectricityConnected>
          <GpsTrackingStatus>string</GpsTrackingStatus>
          <Charging>string</Charging>
          <VoltageLevel>string</VoltageLevel>
          <GSMSignalStrength>string</GSMSignalStrength>
          <AlarmType>string</AlarmType>
          <VehicleNo>string</VehicleNo>
          <Address>string</Address>
          <PacketReceivedAt>string</PacketReceivedAt>
        </LocationPacket>
        <LocationPacket>
          <IMEI>string</IMEI>
          <VehicleId>double</VehicleId>
          <DateTime>dateTime</DateTime>
          <GpsSateliteCount>string</GpsSateliteCount>
          <Latitude>string</Latitude>
          <Longitude>string</Longitude>
          <Speed>string</Speed>
          <IgnitionStatus>string</IgnitionStatus>
          <GasOilElectricityConnected>string</GasOilElectricityConnected>
          <GpsTrackingStatus>string</GpsTrackingStatus>
          <Charging>string</Charging>
          <VoltageLevel>string</VoltageLevel>
          <GSMSignalStrength>string</GSMSignalStrength>
          <AlarmType>string</AlarmType>
          <VehicleNo>string</VehicleNo>
          <Address>string</Address>
          <PacketReceivedAt>string</PacketReceivedAt>
        </LocationPacket>
      </GetVehicleHistoryResult>
    </GetVehicleHistoryResponse>
  </soap:Body>
}