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 的教程,但到目前为止还没有真正“得到它”......

4

1 回答 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);
    }
}

您的connection方法AuthenticateConnection应该返回一些内容以确保用户已通过身份验证。然后您可以在 中使用该值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 ...*/
        }
    }

在这种情况下,异步任务的签名会改变: private class ConnectionTask extends AsyncTask<String, Void, Integer> 并且 doInBackground 应该返回一个Integer.

希望能帮助到你。

于 2012-05-01T09:39:06.437 回答