【发布时间】:2014-12-16 10:13:04
【问题描述】:
我正在开发一个应用程序,我在其中使用 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;
}
}
【问题讨论】:
-
您添加的教程清楚地告诉您如何处理 SOAP 请求和响应,如果您想了解更多信息,请阅读文档...
-
你能发个日志猫吗??
标签: android