2014-12-16 6 views
-2

Я разрабатываю приложение, в котором я обрабатываю веб-службу с помощью запроса и ответа SOAP . Может ли кто-нибудь рассказать мне, как обращаться с запросом SOAP? Я прочитал часть учебника, но этот учебник не совсем понятен. Click hereКак обрабатывать SOAP-запрос и отклик в android

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

возможно дубликат [Как отправить запрос SOAP и Разбираем ответ SOAP в формате XML в Android?] (Http://stackoverflow.com/questions/8767389/how-to-send-soap-request-and -parse-soap-response-in-xml-format-in-android) – DroidDev

+0

Учебное пособие, которое вы добавили, четко говорит вам, как обрабатывать запрос и ответ SOAP, и если вы хотите больше читать документацию ... – Umair

+0

можете ли вы отправить сообщение cat cat ?? – Rohit

ответ

1

Вы должны использовать kSOAP2 баночку. И его документация составляет here.

Для вызова веб-службы, используйте следующие строки

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 используется для отправки тела мыло для веб-службы, envelop.bodyIn используется для получения ответа от веб-службы.

SoapPrimitive resultSOAP = (SoapPrimitive) ((SoapObject) envelope.bodyIn).getProperty(0); 
response=resultSOAP.toString(); 
+0

нормально, но как это будет работать –

+0

U может проверить документацию. Они объяснили это очень красиво – Rohit

+0

, но он не работает для меня –

Смежные вопросы