-2

I'm developing an app in which i m handling the Web Service Using a SOAP request and response. Can any one please tell me how to handle SOAP request and response? I have read some of tutorial but those tutorial is not clearly understood. Click here

public class StockQuoteFetcher {

    private final String NAMESPACE = "https://book.mylimobiz.com/api";
    private final String METHOD_NAME = "GetCars";
    private final String SOAP_ACTION ="https://book.mylimobiz.com/api/GetCars";
    private final String URL = "https://book.mylimobiz.com/api/ApiService.asmx?WSDL";
    private final SoapSerializationEnvelope envelope;

    public StockQuoteFetcher(String apiId, String apikey)
    {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo quotesProperty = new PropertyInfo();
        quotesProperty.setName("apiId");
        quotesProperty.setValue(apiId);
        quotesProperty.setType(String.class);
        request.addProperty(quotesProperty);
        PropertyInfo quotesProperty1 = new PropertyInfo();
        quotesProperty1.setName("apiKey");
        quotesProperty1.setValue(apikey);
        quotesProperty1.setType(String.class);
        request.addProperty(quotesProperty1);

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

    public  List<Car> fetch()
    {
        HttpTransportSE httpRequest = new HttpTransportSE(URL);
        Handler quoteParser = new Handler();;
        try
        {
            httpRequest.call(SOAP_ACTION, envelope);
            SoapObject resultsString = (SoapObject) envelope.getResponse();
            Xml.parse(resultsString.toString(), quoteParser);
            //result =  resultsString.toString();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return quoteParser.getCar();
    }
}

public class Handler extends DefaultHandler {

    private ArrayList<Car> quotes = new ArrayList<Car>();
    private Car currentQuote;
    private String currentNodeText;

    private final String CAR = "Car";
    private final String CAR_ID = "CarId";
    private final String CAR_CODE = "CarCode";
    private final String CAR_NAME = "CarName";
    private final String CAR_TYPE = "CarType";
    private final String CELL_PHONE = "CellPhone";
    private final String TWO_WAY_RADIO_ID = "TwoWayRadioId";

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // Create a new StockQuote for every corresponding
        // <Stock> node in the XML document
        if (localName.equalsIgnoreCase(CAR)) {
            currentQuote = new Car();
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // Retrieve the text content of the current node
        // that is being processed
        currentNodeText = new String(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if(localName.equalsIgnoreCase(CAR_ID)){
            currentQuote.setCarId(CAR_ID);
        }else if(localName.equalsIgnoreCase(CAR_TYPE)){
            currentQuote.setCarType(CAR_TYPE);
        }else if(localName.equalsIgnoreCase(CAR_NAME)){
            currentQuote.setCarName(CAR_NAME);
        }else if(localName.equalsIgnoreCase(CAR)){
            // When the </Stock> element is reached, this quote object is complete.
            quotes.add(currentQuote);
        }
    }

    public ArrayList<Car> getCar()
    {
        return quotes;
    }
}
Umesh Kumar Saraswat
  • 758
  • 3
  • 10
  • 28
  • possible duplicate of [How to send SOAP request and Parse SOAP response in XML format in Android?](http://stackoverflow.com/questions/8767389/how-to-send-soap-request-and-parse-soap-response-in-xml-format-in-android) – DroidDev Dec 16 '14 at 10:26
  • The tutorial you added clearly tells you how to handle SOAP request and response and if you want more read the documentation ... – Umair Dec 16 '14 at 10:30
  • can you please post log cat?? – Rohit Dec 16 '14 at 10:52

1 Answers1

1

You should use kSOAP2 jar. And its documentation is here.

To call web-service, use following lines

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject request = new SoapObject(NAMESPACE, METHOD);
//bodyOut is the body object to be sent out with this envelope
envelope.bodyOut = request;
HttpTransportSE transport = new HttpTransportSE(URL);
transport.call(NAMESPACE + SOAP_ACTION_PREFIX + METHOD, envelope); 

envelope.bodyOut is used to send the soap body to the web service, envelop.bodyIn is used to receive the response from the web service.

SoapPrimitive resultSOAP = (SoapPrimitive) ((SoapObject) envelope.bodyIn).getProperty(0);
response=resultSOAP.toString();
Rohit
  • 2,646
  • 6
  • 27
  • 52
  • U can check documentation. They have explained it very nicely – Rohit Dec 16 '14 at 10:27
  • I got your bug. When I click on any of the web service, it shows me this text `The test form is only available for requests from the local machine`. So please test same code again on emulator instead of testing on mobile – Rohit Dec 16 '14 at 10:53