Android,AsyncTask与kSoap2

use*_*043 4 android ksoap2 android-asynctask

我正在编写一个主要使用从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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的肥皂课是这样的:

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,我如何使用AsyncTask执行此操作?我一直在寻找关于AsyncTask的一些教程,但到目前为止还没有真正"得到它"......

Enr*_*cos 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后叫它:

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

您的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 ...*/
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

希望能帮助到你.