android - 如何在异步任务中调用 ksoap2?

标签 android .net web-services android-asynctask ksoap2

我是安卓开发新手。我正在尝试开发一个应用程序,它将与 .net webservice 连接以检索数据。我想用 AsyncTask 调用 ksoap2。我如何使用 asynctask 将其称为 asyncronus

我的 SoapCall 类是

public class SoapCall {

public final static String SOAP_ACTION = "http://www.alpha.net.com/ExecuteEBSCommand";

public final static String OPERATION_NAME = "ExecuteEBSCommand";

public final static String NAMESPACE = "http://www.alpha.net.com";

public final static String URL = "http://192.168.2.100/Ebs2Alpha/Service.asmx";





public String connection(String Command, String CommandParameters) throws Throwable, Throwable {
    String response = null;
    SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
    Request.addProperty("strCommand", Command);
    Request.addProperty("strCommandParameters", CommandParameters);



    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    soapEnvelope.dotNet = true;
    soapEnvelope.setOutputSoapObject(Request);
    // Needed to make the internet call

    // Allow for debugging - needed to output the request

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        // this is the actual part that will call the webservice
        androidHttpTransport.call(SOAP_ACTION, soapEnvelope);

        // Get the SoapResult from the envelope body.
        SoapObject result = (SoapObject) soapEnvelope.bodyIn;

        response = result.getProperty(0).toString();


    return response;
    }
}

到目前为止,我通过在主要 Activity 中调用连接方法来获得响应

SoapCall  call1= new SoapCall();

call1.connection("get_clients", "%");

最佳答案

使用 AsyncTask 很简单。这是一个例子。

 public class MyTask extends AsyncTask<String, Integer, String>{


    @Override
    protected String doInBackground(String... params) {
    String response = null;
    SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
    Request.addProperty("strCommand", params[0]);
    Request.addProperty("strCommandParameters", params[1]);



    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11);
    soapEnvelope.dotNet = true;
    soapEnvelope.setOutputSoapObject(Request);
    // Needed to make the internet call

    // Allow for debugging - needed to output the request

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.debug = true;
    // this is the actual part that will call the webservice
    androidHttpTransport.call(SOAP_ACTION, soapEnvelope);

    // Get the SoapResult from the envelope body.
    SoapObject result = (SoapObject) soapEnvelope.bodyIn;

    response = result.getProperty(0).toString();


    return response;
  }
}

以及调用带有参数的任务。

MyTask myTask = new MyTask();
myTask.execute(new String[] {Command, CommandParameters});

希望对您有所帮助。

关于android - 如何在异步任务中调用 ksoap2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16483717/

相关文章:

java - 我想显示一个变量值,其中变量名称与连接的字符串匹配

android - 保存 PagerAdapter 的当前状态

c# - 运行我的项目几次后,出现错误 : Could not resolve COM reference "f8937e53-d444-4e71-9275-35b64210cc3b" what's that?

.net - .NET下的C/C++如何在运行时确定方法参数类型?

c# - 哪个速成版用于 C# 中的 ASP.NET Web 应用程序?

java - 摆脱 JAX-WS 中的空 xmlns 元素

java - 我可以使用什么代替 setImageResource() 来处理低于 16 的 API?

java - 当端点非常相似时,路径参数与查询参数

java - webapp 和 web 服务之间的安全通信

android - 缩放图像时出现问题