【问题标题】:How can I make a ksoap2 call in async task?如何在异步任务中进行 ksoap2 调用?
【发布时间】:2013-05-10 13:42:40
【问题描述】:

我是安卓开发的新手。我正在尝试开发一个应用程序,它将与.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;
    }
}

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

SoapCall  call1= new SoapCall();

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

【问题讨论】:

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


    【解决方案1】:

    使用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});
    

    希望它会有所帮助。

    【讨论】:

      【解决方案2】:

      我建议您使用 AsyncTaskLoader,在我看来它比 AsyncTask 更容易。 看看here,这个例子非常广泛,一开始看起来很吓人,你可能会发现更简单的例子。这个想法是您的 Activity 实现 LoaderCallbacks 以创建加载器和加载器完成时调用的方法。你通过LoaderManager '启动' 一个加载器。 AsynctaskLoader 是一个extends AsyncTaskLoader 的类,它执行异步操作。

      我给你举个简单的例子:

      这是 AsyncTaskLoader:

      public class StartupLoader extends AsyncTaskLoader<Boolean> {
      
      Context context;
      
      public StartupLoader(Context context) {
          super(context);
          this.context = context;
          forceLoad();
      
      }
      
      @Override
      public Boolean loadInBackground() {
      
          // DO STUFF!
      
          return true;
      }
      
      @Override
      protected void onStopLoading() {
      
      }
      
      @Override
      public void onCanceled(Boolean data) {
          super.onCanceled(data);
      
      }
      
      @Override
      protected void onReset() {
          super.onReset();
      
      
      
      }
      
      }
      

      这是你在 Activity 中将启动加载器的东西,它是一个内部类:

      public class StartupCallback implements
              LoaderManager.LoaderCallbacks<Boolean> {
          @Override
          public void onLoadFinished(Loader<Boolean> loader, Boolean succ) {
      
              // Here you get your results back
      
          }
      
          @Override
          public Loader<Boolean> onCreateLoader(int id, Bundle args) {
      
              return new StartupLoader(getApplicationContext());
          }
      
          @Override
          public void onLoaderReset(Loader<Boolean> loader) {
      
          }
      }
      

      这就是你从任何你想要的地方(在那个活动中)启动加载器的方式:

      StartupCallback startupCallback = new StartupCallback();
      getSupportLoaderManager().initLoader(0, null, startupCallback);
      

      其中 0 是您为加载程序提供的 ID,null 是一组参数。 祝你好运:)

      【讨论】:

        猜你喜欢
        • 2017-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多