如何在异步任务中进行 ksoap2 调用?
- 作者: 小小小姐好白
- 来源: 51数据库
- 2023-02-13
问题描述
我是安卓开发的新手.我正在尝试开发一个将与 .net webservice 连接以检索数据的应用程序.我想使用 AsyncTask 进行 ksoap2 调用.我如何用 asynctask 调用它asyncronus?
I am a newbie on android development. I am trying to develop an application which will connect with .net webservice in order to retrieve data. I would like to make the ksoap2 call with AsyncTask. How I call it asyncronus with asynctask?
我的 SoapCall 类是
My SoapCall class is
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; } }
到目前为止,我通过调用主要活动中的连接方法获得响应
So far I am getting the response by calling the connection method in main activity with
SoapCall call1= new SoapCall(); call1.connection("get_clients", "%");
推荐答案
使用 AsyncTask 很简单.这是一个例子.
Using AsyncTask is straightforward. Here is an example.
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; } }
以及对带参数的任务的调用.
And the call to the task with parameters.
MyTask myTask = new MyTask(); myTask.execute(new String[] {Command, CommandParameters});
希望它会有所帮助.
推荐阅读
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0