【问题标题】:how to use asynctask properly with ksoap2 and axis2 web service如何在 ksoap2 和 axis2 Web 服务中正确使用 asynctask
【发布时间】:2013-06-06 19:08:43
【问题描述】:

我使用 Web 服务向导从一个动态 Web 项目开发了一个 Web 服务(按照教程进行操作,所以我不确定自己在做什么)。该教程让我安装了axis2插件,所以我假设这是用于Web服务生成的插件。无论如何,当我从浏览器尝试该服务时,该服务正在运行,因此该部分没有问题。

我的问题是,我想从一个安卓应用程序访问这个网络服务。这两天看了很多教程,尝试实现一个,后来发现已经过时了,尝试在主线程中连接网络,android 3.0以后不允许。然后我发现了 asynctask 并尝试了它,但我不明白的地方太多了。我找到的教程没有解释任何这些,所以我在这里问。

我在听说异步任务之前写的代码在这里:

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive; 
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

private final String NAMESPACE = "http://eko.erdem.com";
private final String URL = "http://159.20.89.38:9398/EkoBiletTest/services/EkoClass?wsdl";
private final String SOAP_ACTION = "http://eko.erdem.com/homePage";
private final String METHOD_NAME = "homePage";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    String place = "Paris";
    String checkinDate = "2013-06-10";
    String checkoutDate = "2013-06-12";

  //Pass value for place variable of the web service
    PropertyInfo placeProp =new PropertyInfo();
    placeProp.setName("place");//Define the variable name in the web service method
    placeProp.setValue(place);//Define value for place variable
    placeProp.setType(String.class);//Define the type of the variable
    request.addProperty(placeProp);//Pass properties to the variable

  //Pass value for checkinDate variable of the web service
    PropertyInfo checkinDateProp =new PropertyInfo();
    checkinDateProp.setName("checkinDate");
    checkinDateProp.setValue(checkinDate);
    checkinDateProp.setType(String.class);
    request.addProperty(checkinDateProp);


  //Pass value for checkinDate variable of the web service
    PropertyInfo checkoutDateProp =new PropertyInfo();
    checkoutDateProp.setName("checkoutDate");
    checkoutDateProp.setValue(checkoutDate);
    checkoutDateProp.setType(String.class);
    request.addProperty(checkoutDateProp);



    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();


        TextView tv = (TextView) findViewById(R.id.textView);
        tv.setText(response.toString());
        setContentView(tv);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

现在我不知道代码的哪些部分应该在asynctask的哪个函数中(doinBackground()等)我也不了解asynctask的参数发送系统。例如,如果我想将 NAMESPACE、URL、METHOD_NAME 等传递给 asynctask,我应该怎么做?另外我应该在哪里更新主要活动的 textView?如果有人可以向我指出涵盖这些问题的教程,那就太好了。我也将不胜感激您对这些问题的任何回答。提前致谢。

【问题讨论】:

    标签: java android web-services android-asynctask android-ksoap2


    【解决方案1】:

    任何网络相关的工作都需要在AsyncTaskdoInBackground方法中完成

    要将数据发送到异步任务,您需要为类声明传入数据

    puclic class MyNetowrkTask extends AsyncTask<String[],Void,Void>
    

    &lt;&gt;中的第一个参数是传入的数据,第二个是进度,第三个是任务完成后返回onPostExecute的结果。

    所以要传递字符串,你会做这样的事情

    new MyNetworkTask().execute(new String[] {"namespace","url","method"});
    

    doInBackground 你会得到这样的数据

    protected Void doInBackground(String... params) {
        String[] data = params[0];
        String namespace = data[0];
        String url = data[1];
        String method = data[2];
        ....
    }
    

    UI 元素的任何更新都需要在 onPostExecute 中完成,因此如果您想更新 textview,则需要在此处完成。

    代码并不完全正确,但这会让你开始理解这个想法

    【讨论】:

      【解决方案2】:

      现在我不知道代码的哪些部分应该在 asynctask 的哪个函数中(doinBackground() 等)

      基本上,将大部分工作放在doInBackground() 中,并确保不要尝试从那里更新UI。任何其他方法都可以更新UIHere is an answer 显示基本结构。

      另外我不明白asynctask的参数发送系统。例如,如果我想将 NAMESPACE、URL、METHOD_NAME 等传递给 asynctask,我应该怎么做?

      如果您希望在doInBackground() 中使用它们,您可以将execute() 函数中的那些传递为vargs,否则您可以在构造函数中传递它们

      MyTask task = new MyTask();  // pass variables here to go to the constructor`
      task.execute()  // anything passed here will be received by doInBackground()`
      

      我应该在哪里更新主要活动的 textView?

      这可以通过除doInBackground() 之外的任何方法完成,具体取决于您需要更新它的时间(在任务完成之前、期间或之后)

      如果这是您的Activity 的内部类,那么您可以将该Activity 的成员变量与Context 一起用于任务。如果它是一个单独的文件,那么您可能需要将Context 传递给您的任务的constructor

      【讨论】:

        【解决方案3】:

        您应该将执行 HTTP 请求的代码(可能是 try-catch 块中的任何代码)放入异步任务的 doInBackground() 方法中。如果您想将各种参数传递给它,您可以将它们封装到一个对象中并传递该对象。或者,您可以通过构造函数将它们传递给您的异步任务,并在 doInBackground() 中使用它们。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多