Java Android-选择在线程中运行的方法

Java Android-选择在线程中运行的方法,java,android,multithreading,java-threads,android-threading,Java,Android,Multithreading,Java Threads,Android Threading,我正在创建一个与Web服务通信的Android应用程序 我有一个SOAP类,它包含执行web服务函数的不同方法,还有一个SOAPCaller类,它运行一个线程来调用SOAP类中的方法 为了选择SOAPCaller中的线程调用哪个方法,我设置了一个带有方法名称的字符串,在我的应用程序中,我的run()方法有一个switch语句,它查看该字符串以决定应该调用SOAP中的哪个方法 这是很好的工作,但只是感觉非常黑客。。。有更好的方法吗?当线程以某种方式运行时,我可以在SOAPCaller类中调用所需的

我正在创建一个与Web服务通信的Android应用程序

我有一个SOAP类,它包含执行web服务函数的不同方法,还有一个SOAPCaller类,它运行一个线程来调用SOAP类中的方法

为了选择SOAPCaller中的线程调用哪个方法,我设置了一个带有方法名称的字符串,在我的应用程序中,我的run()方法有一个switch语句,它查看该字符串以决定应该调用SOAP中的哪个方法

这是很好的工作,但只是感觉非常黑客。。。有更好的方法吗?当线程以某种方式运行时,我可以在SOAPCaller类中调用所需的方法吗

感谢您的帮助或建议。谢谢

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();
    }
}
肥皂剧

 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();
 }