使用 K-SOAP for Android 的示例代码。
private void sendSOAPmsg(DamageAssessmentFormPojo pojo) throws IOException, XmlPullParserException, SoapFault {
SoapObject request = new SoapObject(WEBSERVICE.NAMESPACE, WEBSERVICE.METHOD_NAME_SUBMIT_REPORT);
request.addProperty("xmlBytes", Util.getSoapBase64String(pojo));
request.addProperty("fileName", IO.DefaultReportName);
request.addProperty("deviceId", AppConstants.IMEI != null ? AppConstants.IMEI : Util.getIMEI(this));
SoapPrimitive response = sendSOAPEnvelope(request, WEBSERVICE.SOAP_ACTION_SUBMIT_REPORT);
if (response.toString().equalsIgnoreCase("true")) {
Logger.logInfo("REPORT SENT SUCCESSFULLY", "WEB-SERVICE");
pojo.setReportSent(true);
IO.writeObject(pojo.getReportsFolderPath() + IO.DefaultReportName, pojo);
} else {
Logger.logInfo("REPORT SENT FAILED", "WEB-SERVICE - RESONSE " + response.toString());
}
}
private SoapPrimitive sendSOAPEnvelope(SoapObject request, String soapAction) throws IOException, XmlPullParserException, SoapFault {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(WEBSERVICE.URL);
androidHttpTransport.call(soapAction, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
return response;
}
private boolean sendSOAPimage(String strImg, File pFile) throws IOException, XmlPullParserException {
boolean result = false;
if (strImg != null) {
SoapObject request = new SoapObject(WEBSERVICE.NAMESPACE, WEBSERVICE.METHOD_NAME_SUBMIT_IMAGE);
request.addProperty("imageBytes", strImg);
request.addProperty("fileName", pFile.getName());
request.addProperty("deviceId", AppConstants.IMEI != null ? AppConstants.IMEI : Util.getIMEI(this));
SoapPrimitive response = sendSOAPEnvelope(request, WEBSERVICE.SOAP_ACTION_SUBMIT_MAGE);
if (response.toString().equalsIgnoreCase("true")) {
result = true;
Logger.logInfo("IMAGE SENT SUCCESSFULLY", "WEB-SERVICE");
} else {
Logger.logInfo("IMAGE SENT FAILED", "WEB-SERVICE - RESONSE " + response.toString());
}
}
return result;
}
//--------Util 辅助方法
public static String getSoapBase64String(DamageAssessmentFormPojo pojo) {
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try {
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.startTag("", XMLTags.TAG_ROD);
xmlSerializer.startTag("", XMLTags.TAG_ORDER);
xmlSerializer.startTag("", XMLTags.TAG_SEVERITY);
xmlSerializer.text(pojo.getCheckedSeverity_Complexity());
xmlSerializer.endTag("", XMLTags.TAG_SEVERITY);
xmlSerializer.startTag("", XMLTags.TAG_DAMAGE_TYPE);
StringBuilder builder = new StringBuilder();
for (String str : pojo.getCheckedDamageTypes()) {
builder.append(str + " , ");
}
xmlSerializer.text(builder.toString());
xmlSerializer.endTag("", XMLTags.TAG_DAMAGE_TYPE);
xmlSerializer.startTag("", XMLTags.TAG_ENV_IMPACT);
xmlSerializer.text(pojo.getCheckedEnvImpact());
xmlSerializer.endTag("", XMLTags.TAG_ENV_IMPACT);
xmlSerializer.startTag("", XMLTags.TAG_ENV_COMMENT);
xmlSerializer.text(pojo.getEnvImpactComments());
xmlSerializer.endTag("", XMLTags.TAG_ENV_COMMENT);
xmlSerializer.startTag("", XMLTags.TAG_TRAVEL_CONDITION);
xmlSerializer.text(pojo.getCheckedTravelCond());
xmlSerializer.endTag("", XMLTags.TAG_TRAVEL_CONDITION);
xmlSerializer.startTag("", XMLTags.TAG_TRAVEL_COMMENT);
xmlSerializer.text(pojo.getTravCondComments());
xmlSerializer.endTag("", XMLTags.TAG_TRAVEL_COMMENT);
xmlSerializer.startTag("", XMLTags.TAG_ORDER_DATE);
xmlSerializer.text(pojo.getDateTime());
xmlSerializer.endTag("", XMLTags.TAG_ORDER_DATE);
xmlSerializer.endTag("", "Order");
xmlSerializer.endTag("", "ROD");
xmlSerializer.endDocument();
} catch (IllegalArgumentException e) {
Logger.logException(e);
} catch (IllegalStateException e) {
Logger.logException(e);
} catch (IOException e) {
Logger.logException(e);
}
return Base64.encode(writer.toString().getBytes());
}