我正在创建一个与 Web 服务通信的 Android 应用程序。
我有一个 SOAP 类,它包含执行 Web 服务功能的不同方法,还有一个 SOAPCaller 类,它运行一个线程来调用 SOAP 类中的方法。
要选择 SOAPCaller 中的线程调用哪个方法,我在我的应用程序中的任何位置设置一个带有方法名称的字符串,然后我的 run() 方法有一个 switch 语句,它查看字符串以决定从哪个方法应该调用 SOAP。
这工作正常,但感觉很hacky......有没有更好的方法来做到这一点?当线程以某种方式运行时,我可以在我的 SOAPCaller 类中调用所需的方法吗?
任何帮助或建议表示赞赏。谢谢!
首页片段
public void Addition()
{
try {
//get values from ui
int valueA = Integer.parseInt(etIntA.getText().toString());
int valueB = Integer.parseInt(etIntB.getText().toString());
//new soap caller
SOAPCaller soapCaller = new SOAPCaller();
//set method (this is how I tell the thread which method to run)
soapCaller.setMethod("Addition");
//set the values to be used by the addition method
soapCaller.setAdditionValues(valueA, valueB);
//start the thread
soapCaller.setStartThread(true);
//start soap caller thread
soapCaller.join();
soapCaller.start();
//loop until result is updated
while (soapCaller.getStartThread()) {
try {
Thread.sleep(10);
}
catch(Exception ex) {
}
}
//show the result on screen
tvResult.setText(soapCaller.getResult());
}
catch(Exception ex) {
//show error on screen
Toast.makeText(getActivity(), "Error: " + ex.toString(),Toast.LENGTH_SHORT).show();
}
}
SOAP调用者:
private String method;
private String result;
public void run() {
try {
//new soap actions class
soap = new SOAP();
switch (method) {
case "Addition":
//call addition action, pass given values and get the result
result = soap.Addition(additionA, additionB);
startThread = false;
break;
case "InsertEmployee":
//pass values and insert new employee into database
result = soap.InsertEmployee(insertName, insertDob, insertRole);
startThread = false;
break;
case "SelectEmployee":
//pass values and insert new employee into database
result = soap.SelectEmployee(selectEmployeeName);
startThread = false;
break;
case "ConnectionTest":
//test the webservice connection
result = soap.ConnectionTest();
startThread = false;
break;
}
}
catch (Exception ex)
{
//error occurred
result = ex.toString();
}
}
肥皂
public String Addition(int a, int b) {
//new request and property
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME_ADDITION);
PropertyInfo propertyInfo;
//add integer a into property info
propertyInfo = new PropertyInfo();
propertyInfo.setName("valueA");
propertyInfo.setValue(a);
propertyInfo.setType(Integer.class);
request.addProperty(propertyInfo);
//add integer b into property info
propertyInfo = new PropertyInfo();
propertyInfo.setName("valueB");
propertyInfo.setValue(b);
propertyInfo.setType(Integer.class);
request.addProperty(propertyInfo);
//new soap serialisation envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
//set http transport as webservice address
HttpTransportSE httpTransportSE = new HttpTransportSE(SOAP_ADDRESS);
//object to hold result
Object response = null;
try {
//call SOAP action and get response
httpTransportSE.call(SOAP_ACTION_ADDITION, envelope);
response = envelope.getResponse();
}
catch (Exception ex) {
//get error
response = ex.toString();
}
//return result
return response.toString();
}