public class SoapSend implements Callable<SoapResponse> {
private SoapRequest soapRequest;
public SoapSend(SoapRequest soapRequest) {
this.soapRequest = soapRequest;
}
@Override
public SoapResponse call() {
Object result = sendSoap(soapRequest);
if (result != null && result instanceof SoapObject) {
return new SoapResponse(result);
}
return null;
}
private Object sendSoap(SoapRequest request) {
try {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request.getParameters();
envelope.dotNet = true;
envelope.setOutputSoapObject(request.getParameters());
new MarshalBase64().register(envelope);
int out_times = 20000;
HttpTransportSE transport = new HttpTransportSE(request.getURL(), out_times);
transport.call(request.getSoapAction(), envelope);
return envelope.getResponse();
} catch (IOException | XmlPullParserException e) {
soapRequest.onNetError(e);
}
return null;
}
}
public abstract class SoapRequest{
private String URL;
private String nameSpace;
private String methodName;
private String soapAction;
private SoapObject parameters;
public abstract void onNetError(Exception e);
private ExecutorService executorService = Executors.newCachedThreadPool();
protected SoapRequest(String URL, String nameSpace, String methodName) {
this.URL = URL;
this.nameSpace = nameSpace;
this.methodName = methodName;
this.soapAction = nameSpace + methodName;
parameters = new SoapObject(nameSpace, methodName);
}
protected void addParameter(String name, boolean value) {
addParameter(name, String.valueOf(value));
}
protected void addParameter(String name, int value) {
addParameter(name, String.valueOf(value));
}
protected void addParameter(String name, String value) {
if (name == null || value == null) {
return;
}
parameters.addProperty(name, value);
}
public SoapObject getParameters() {
return parameters;
}
public String getSoapAction() {
return soapAction;
}
public String getURL() {
return URL;
}
public SoapResponse sendRequest() {
try {
FutureTask<SoapResponse> futureTask = new FutureTask<>(new SoapSend(this));
executorService.submit(futureTask);
return futureTask.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return null;
}
}