简体   繁体   English

如何在ksoap2和axis2 Web服务中正确使用asynctask

[英]how to use asynctask properly with ksoap2 and axis2 web service

I developed a web service from a dynamic web project using the web service wizard(followed a tutorial so I wasn't really sure what I was doing). 我使用Web服务向导从动态Web项目开发了Web服务(遵循了教​​程,因此我不确定自己在做什么)。 The tutorial made me install the axis2 plugin so I'm assuming that's the one used for the web service generation. 本教程使我安装了axis2插件,因此我假设这是用于Web服务生成的插件。 Anyhow, the service is working when I tried it from my browser so no problem in that part. 无论如何,当我从浏览器中尝试该服务时,该服务正在运行,因此该部分没有问题。

My problem is, I want to access this web service from an android application. 我的问题是,我想从android应用程序访问此Web服务。 I read a lot of tutorials past two days and tried to implement one, but later realized that it was outdated and it tried to connect to network in the main thread, which is not allowed after android 3.0. 我在过去两天里读了很多教程,并尝试实现其中一个,但是后来意识到它已经过时了,它试图在主线程中连接到网络,而在android 3.0之后是不允许的。 Then I found out about asynctask and tried my hand in it but there is just so much that I don't understand. 然后我发现了有关asynctask的问题,并尝试了一下,但是有很多我不理解的地方。 The tutorials I found didn't explain any of those so I'm asking here. 我发现的教程没有任何解释,所以我在这里问。

The code I wrote before hearing about the asynctask is here: 我在听说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;
}

}

Now I don't know which parts of the code should be in which function of the asynctask ( doinBackground() etc.) Also I don't understand the parameter sending system of asynctask. 现在我不知道代码的哪一部分应该在asynctask的哪个函数中(doinBackground()等)。我也不了解asynctask的参数发送系统。 For example if I wanted to pass the NAMESPACE, URL, METHOD_NAME etc. to the asynctask how should I do it? 例如,如果我想将NAMESPACE,URL,METHOD_NAME等传递给asynctask,该怎么办? Also where should I update the textView of the main activity? 另外,我应该在哪里更新主要活动的textView? If someone can point me to a tutorial that covers these questions it would be great. 如果有人可以指出涉及这些问题的教程,那就太好了。 I would also appreciate any answer of your own to those questions. 对于您对这些问题的任何回答,我也将不胜感激。 Thanks in advance. 提前致谢。

Any network related work needs to be done in the doInBackground method of the AsyncTask 任何与网络相关的工作都需要在AsyncTaskdoInBackground方法中完成

to send data to the asynctask you need to declare the incoming data for the class 要将数据发送到asynctask,您需要声明该类的传入数据

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

the first parameter in the <> is the imcoming data, the second is the progress and the thirs is the result you return to the onPostExecute when the task is done. <>的第一个参数是即将到来的数据,第二个参数是进度,第三个是完成任务后返回到onPostExecute的结果。

so to pass strings in you would do something like this 所以要传递字符串,你会做这样的事情

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

the in the doInBackground you get the data like this doInBackground您将获得像这样的数据

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

Any update of a UI element needs to be done in the onPostExecute so if you want to update a textview it needs to be done there. UI元素的任何更新都需要在onPostExecute中完成,因此,如果要更新textview,则需要在此处进行。

the code isnt exactly right but this will get you started to get the idea 代码并不完全正确,但这会让您开始了解

Now I don't know which parts of the code should be in which function of the asynctask ( doinBackground() etc.) 现在我不知道代码的哪个部分应该在asynctask的哪个函数中(doinBackground()等)。

Basically, put the bulk of the work in doInBackground() and make sure not to try and update the UI from there. 基本上,将大部分工作放在doInBackground() ,并确保不要尝试从那里更新UI Any other of the methods can update the UI . 其他任何方法都可以更新UI Here is an answer that shows the basic structure. 这是显示基本结构的答案

Also I don't understand the parameter sending system of asynctask. 我也不了解asynctask的参数发送系统。 For example if I wanted to pass the NAMESPACE, URL, METHOD_NAME etc. to the asynctask how should I do it? 例如,如果我想将NAMESPACE,URL,METHOD_NAME等传递给asynctask,该怎么办?

You can pass those in the execute() function as vargs if you want those to be used in doInBackground() otherwise you can pass them in the constructor 如果要在doInBackground()使用这些参数,可以将它们作为vargsexecute()函数中传递,否则可以在构造函数中传递它们

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

Also where should I update the textView of the main activity? 另外,我应该在哪里更新主要活动的textView?

This can be done in any methods besides doInBackground() depending on when you need to update it (before, during, or after) the task finishes 可以使用doInBackground()之外的任何方法来完成此操作,具体取决于您何时需要更新它(任务完成之前,之中或之后)

If this is an inner class of your Activity then you have the member variables of that Activity available to the task along with Context . 如果这是一个内部类的的Activity ,那么你有一个成员变量Activity提供给任务沿着Context If it is a separate file then you may need to pass Context to the constructor of your task. 如果它是一个单独的文件,则可能需要将Context传递给任务的constructor

You should put the code which performs the HTTP request (probably whatever is inside your try-catch block) into the doInBackground() method of the async task. 您应该将执行HTTP请求的代码(可能在try-catch块中的任何内容)放入异步任务的doInBackground()方法中。 If you want to pass various parameters on to it you can encapsulate them into an object and pass the object. 如果要传递各种参数,可以将它们封装到一个对象中并传递该对象。 Alternatively you can pass them through a constructor to your async task and have them used in doInBackground(). 或者,您可以将它们通过构造函数传递给异步任务,并在doInBackground()中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM