英文:
Android - Choosing a method to run in a thread
问题
我正在创建一个与网络服务通信的Android应用程序。
我有一个SOAP类,其中包含执行网络服务函数的不同方法,以及一个SOAPCaller类,该类运行一个线程来调用SOAP类中的方法。
为了选择在SOAPCaller中的线程调用哪个方法,我正在设置一个字符串,其中包含方法的名称,无论在我的应用程序中的任何地方,我的run()方法都有一个switch语句,用于查看字符串,以决定调用SOAP中的哪个方法。
虽然这样做可以工作,但感觉非常不正规... 是否有更好的方法来做到这一点?我能在线程运行时以某种方式直接调用SOAPCaller类中所需的方法吗?
任何帮助或建议都将不胜感激。谢谢!
HomeFragment
public void Addition() {
try {
//从界面获取值
int valueA = Integer.parseInt(etIntA.getText().toString());
int valueB = Integer.parseInt(etIntB.getText().toString());
//创建新的SOAPCaller
SOAPCaller soapCaller = new SOAPCaller();
//设置方法(这是我告诉线程要运行哪个方法的方式)
soapCaller.setMethod("Addition");
//设置要由加法方法使用的值
soapCaller.setAdditionValues(valueA, valueB);
//启动线程
soapCaller.setStartThread(true);
//启动SOAPCaller线程
soapCaller.join();
soapCaller.start();
//循环,直到结果被更新
while (soapCaller.getStartThread()) {
try {
Thread.sleep(10);
} catch (Exception ex) {
}
}
//在屏幕上显示结果
tvResult.setText(soapCaller.getResult());
} catch (Exception ex) {
//在屏幕上显示错误
Toast.makeText(getActivity(), "错误:" + ex.toString(), Toast.LENGTH_SHORT).show();
}
}
SOAPCaller:
private String method;
private String result;
public void run() {
try {
//创建新的SOAPActions类
soap = new SOAP();
switch (method) {
case "Addition":
//调用加法操作,传递给定的值并获取结果
result = soap.Addition(additionA, additionB);
startThread = false;
break;
case "InsertEmployee":
//传递值并将新员工插入数据库
result = soap.InsertEmployee(insertName, insertDob, insertRole);
startThread = false;
break;
case "SelectEmployee":
//传递值并在数据库中选择新员工
result = soap.SelectEmployee(selectEmployeeName);
startThread = false;
break;
case "ConnectionTest":
//测试网络服务连接
result = soap.ConnectionTest();
startThread = false;
break;
}
} catch (Exception ex) {
//发生错误
result = ex.toString();
}
}
SOAP
public String Addition(int a, int b) {
//创建新的请求和属性
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME_ADDITION);
PropertyInfo propertyInfo;
//将整数a添加到属性信息中
propertyInfo = new PropertyInfo();
propertyInfo.setName("valueA");
propertyInfo.setValue(a);
propertyInfo.setType(Integer.class);
request.addProperty(propertyInfo);
//将整数b添加到属性信息中
propertyInfo = new PropertyInfo();
propertyInfo.setName("valueB");
propertyInfo.setValue(b);
propertyInfo.setType(Integer.class);
request.addProperty(propertyInfo);
//创建新的SOAP序列化包
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
//将http传输设置为网络服务地址
HttpTransportSE httpTransportSE = new HttpTransportSE(SOAP_ADDRESS);
//用于保存结果的对象
Object response = null;
try {
//调用SOAP操作并获取响应
httpTransportSE.call(SOAP_ACTION_ADDITION, envelope);
response = envelope.getResponse();
} catch (Exception ex) {
//获取错误
response = ex.toString();
}
//返回结果
return response.toString();
}
英文:
I'm creating an Android app that communicates with a webservice.
I have a SOAP class that holds different methods which execute web service functions and a SOAPCaller class that runs a thread to call the methods within the SOAP class.
To choose which method is called by the thread in SOAPCaller, I am setting a string with the name of the method from where ever in my app, my run() method then has a switch statement which looks at the string to decide which method from SOAP should be called.
This is working fine but just feels very hacky... is there a better way of doing this? Can I just call the desired method within my SOAPCaller class when the thread runs somehow?
Any help or advice appreciated. Thanks!
HomeFragment
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();
}
}
SOAPCaller:
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();
}
}
SOAP
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();
}
专注分享java语言的经验与见解,让所有开发者获益!
评论