1

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

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 肥皂 = 新 Main_itemsList().execute();

我们的

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

谢谢..

4

1 回答 1

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. 在那里,您获取结果并将其“发布”到您的活动或片段。

于 2012-12-20T13:10:11.030 回答