1

我是android开发的新手。我正在尝试开发一个应用程序,该应用程序将与.net webservice 连接以检索数据。我想用 进行ksoap2调用AsyncTask。我如何用 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", "%");
4

2 回答 2

2

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

希望它会有所帮助。

于 2013-05-10T14:06:30.717 回答
0

我建议您使用 AsyncTaskLoader,根据我的口味,它比 AsyncTask 更容易。看看这里,这个例子非常广泛,一开始看起来很吓人,你可能会发现更简单的例子。这个想法是您的 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 是一组参数。祝你好运 :)

于 2013-05-10T14:02:08.597 回答