【问题标题】:how to get soapObjet return in AsyncTask with kSoap2 in android如何在android中使用kSoap2在AsyncTask中获取soapObjet返回
【发布时间】:2012-12-20 12:38:57
【问题描述】:

我想调用 ASMX 网络服务, 如何从 doInBackground 取回活动中的 SoapObject 或 List ,网络服务工作正常,它在日志文件中显示值 ,我的代码是,我调用网络服务的文件

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;

public class WebserviceMainCategory {


    static String SOAP_ACTION = "http://tempuri.org/loadProductCatagory";
    static String METHOD_NAME = "loadProductCatagory";
    static String NAME_SPACE = "http://tempuri.org/";
    static String URL = "http://74.53.87.146/seharwebservice/Service.asmx";
    public  List<String> main_items = new ArrayList<String>();

    public static String[] namess ;
    static int s ;
    public static SoapObject soap;

    public  SoapObject webServiceMain_list() {

        SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.implicitTypes = true;
        envelope.setAddAdornments(false);
        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransportSe = new HttpTransportSE(URL);
        httpTransportSe.debug = true;


        try {

            httpTransportSe.call(SOAP_ACTION, envelope);

            Object result = envelope.getResponse();

            soap = (SoapObject) result;

            /*
            SoapObject result2nd = (SoapObject) result;
            s = result2nd.getPropertyCount();
                        //String[]arrayLis = new String[s];

                        namess = new String[s];
                        for(int i=0; i<result2nd.getPropertyCount(); i++)
                        {
                            Log.d("ddd", result2nd.getPropertyAsString(i));
                            namess[i]=result2nd.getPropertyAsString(i);
                            Log.d("array", namess[i]);
                            main_items.add(result2nd.getPropertyAsString(i));
                        }
                    */


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

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return soap;    
    }

}

我在哪里创建 AsyncTask 类

class Main_itemsList extends AsyncTask<Void, Void,  SoapObject> {

    @Override
    protected SoapObject doInBackground(Void... params) {
        WebserviceMainCategory mAuth = new WebserviceMainCategory();

        return  mAuth.webServiceMain_list();

    }

    public void execute(SoapObject aa) {


    }
    }

如何在主要活动中获得响应,

喜欢我活动中的代码

SoapObject soap = new Main_itemsList().execute();

我们的

ListArray aa = new Main_itemsList().execute();

谢谢..

【问题讨论】:

    标签: android android-asynctask asmx


    【解决方案1】:

    你喜欢这样做

    private SoapObject aa;
    class Main_itemsList extends AsyncTask<Void, Void, SoapObject> {
        @Override
        protected SoapObject doInBackground(Void... params) {
            // this is executed in a background thread.
            // the result is returned to the UI thread via onPostExecute
            WebserviceMainCategory mAuth = new WebserviceMainCategory();
            return  mAuth.webServiceMain_list();
        }
        @Override
        protected void onPostExecute(SoapObject result) {
            // aa is inside your Activity / Fragment which
            // contains this class
            aa = result;
        }
    }
    
    // you just need to start the task somewhere
    public void onSomething() {
        // this will set "aa" when it's done.
        new Main_itemsList().execute();
    }
    

    如果您启动AsyncTask,它将在后台线程中执行您在doInBackground 中定义的任何操作。您返回的结果将被传输回 UI 线程到onPostExecute。在那里,您获取结果并将其“发布”到您的活动或片段。

    【讨论】:

    • 感谢您的回复..我制作了一个静态 SoapObject ..当我调用函数时它没有给我错误但是当我访问 aa SoapObject 时它给我错误,即使它不打印值日志文件中的 aa 新 Main_itemsList().execute(); for(int i=0; i
    • 您无法立即访问aa,因为直到AsyncTask 在未来某个时间点完成后才会设置它。如果要打印,请从onPostExecute 触发
    • 一个问题 .. 我想调用 Web 服务进行登录,获取 asmx Web 服务的用户名和密码,如何强制延迟一段时间以获取 Web 服务的用户名和密码……我打电话给睡眠但应用程序崩溃.. 谢谢
    • 如果您执行异步操作,您不会等待它们发生,而是使用异步操作完成时发生的事件来触发所有更新。在你的情况下:开始登录任务,然后什么都不做,只是让 UI 处于“请等待我正在加载东西”状态,一旦加载完成,你再次更新 UI 以继续
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多