我正在开发一个应用程序,其中我使用 SOAP 请求和响应来处理Web服务。任何人都可以告诉我如何处理 SOAP 请求和响应?我已经阅读了一些教程,但这些教程并没有被清楚地理解。 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;
}
}
答案 0 :(得分:1)
要调用网络服务,请使用以下行
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用于将soap主体发送到Web服务,envelop.bodyIn用于接收来自Web服务的响应。
SoapPrimitive resultSOAP = (SoapPrimitive) ((SoapObject) envelope.bodyIn).getProperty(0);
response=resultSOAP.toString();