Android, AsyncTask with kSoap2

血红的双手。 提交于 2019-12-04 09:15:27

You can do:

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();
        }
    }
}

And then call it:

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

Your connection method in AuthenticateConnection should return something to ensure the user has been authenticated. Then you can use that value in the onPostExecute, something like this:

    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 ...*/
        }
    }

In this case the signature of the asynctask will change: private class ConnectionTask extends AsyncTask<String, Void, Integer> and the doInBackground should return an Integer.

Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!