Android,使用kSoap2的AsyncTask

4
我正在编写一个应用程序,主要使用从Web服务获取的数据,并希望使用AsyncTask在后台运行SOAP调用...我是Android方面的新手(是一个iOS程序员),所以我对此还有些陌生...
现在,我有一个登录界面,在那里我获取用户提供的登录信息并将其与服务器上的信息进行匹配...
因此,在我的登录活动中:
    loginBtn.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            //Run the connection to authenticate the user
            AuthenticateConnection mAuth = new AuthenticateConnection();

            mAuth.mNumber = number;
            mAuth.mPassword = pass;

            mAuth.connection();
        }
    }

我的肥皂类如下所示:

public class AuthenticateConnection
{
    private static final String SOAP_ACTION = "http://tempuri.org/Authenticate";
    private static final String METHOD_NAME = "Authenticate";
    private static final String NAMESPACE = "http://tempuri.org/";
    private String URL;

    public Boolean userOK;

    public String mNumber;
    public String mPassword;

    public AuthenticateConnection()
    {

    }

    public void connection()
    {
        Singleton service = Singleton.getInstance();
        String firstURL = service.getURL();
        URL = firstURL + "Parent.svc";

        System.out.println("Connection to: " + URL);

        //Initialize soap request
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        //Add parameters
        request.addProperty("login", mNumber);
        request.addProperty("password", mPassword);

        //Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.dotNet=true;
        envelope.implicitTypes=true;
        envelope.setAddAdornments(false);

        //Prepare request
        envelope.setOutputSoapObject(request);

        //Needed to make the internet call
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        //Allow for debugging - needed to output the request
        androidHttpTransport.debug = true;

        try 
        {
            //this is the actual part that will call the web service
            androidHttpTransport.call(SOAP_ACTION, envelope);

            //Get the SoapResult from the envelope body.
            //Object result = envelope.getResponse();
            //Object result = envelope.bodyIn;
            SoapObject sResult = (SoapObject)envelope.bodyIn;

            String tempID = sResult.getProperty("AuthenticateResult").toString();

            //Check if the user exists and has the correct password
            if(tempID != "-1")
            {
                userOK = true;

                //Store the values in the singleton class
                service.parentID = sResult.getProperty("AuthenticateResult").toString();
                service.parentToken = sResult.getProperty("token").toString();
            }

            //If -1 is returned, then either the number or the password is incorrect
            else
            {
                userOK = false;
            }           
        } catch(org.xmlpull.v1.XmlPullParserException ex2)
        {               
            //System.out.println(androidHttpTransport.requestDump.toString());

        }  catch (Exception e)
        {
            e.printStackTrace();
            System.out.println(androidHttpTransport.requestDump.toString());
        }
    }
}

我的问题是,我该如何使用AsyncTask实现这个功能呢? 我看了一些关于AsyncTask的教程,但至今仍不太理解...

1个回答

6

以下是可以做的操作:

private class ConnectionTask extends AsyncTask<String, Void, Void> {
    private ProgressDialog dialog = new ProgressDialog(ACTIVITY_NAME.this);

    protected void onPreExecute() {
        dialog.setMessage("Connecting...");
        dialog.show();
    }

    protected void doInBackground(String... args) {
        AuthenticateConnection mAuth = new AuthenticateConnection();
        mAuth.mNumber = args[0];
        mAuth.mPassword = args[1];
        mAuth.connection();
    }

    protected void onPostExecute(Void v) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

然后调用它:

loginBtn.setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {
        //Run the connection to authenticate the user
        new ConnectionTask().execute(number, pass);
    }
}

您的AuthenticateConnection中的connection方法应该返回某个值以确保用户已通过身份验证。然后您可以在onPostExecute中使用该值,类似于以下内容:

    protected void onPostExecute(Integer res) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        if (res.intValue() == OK) {
            /* Maybe start a new Activity ...*/
        } else {
            /* Maybe show a Toast with an error message ...*/
        }
    }

在这种情况下,asynctask的签名将会改变: private class ConnectionTask extends AsyncTask<String, Void, Integer> 并且doInBackground应该返回一个Integer
希望这可以帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接