开发者

Show 'please wait' during web service call

I have an activity which checks username availability via .net web services, I need to show a 'please wait' message during the call as it can take a second or two - I have experimented with AsyncTask and Threading but the problem I have is I actually want to block the main (and only) thread until the web service call has completed as I am depending on the result from it to process other logic.

I have tried allsorts but no joy - can anyone help please?

As requested, here is a section of my code :

private OnClickListener RegistrationListener = new OnClickListener() {
    public void onClick(View v) {
        ClearFieldHighlighting();
        if (ValidateData()) {
            if (IsOnline()) {
                if (UsernameIsAvailable()) {
                    try {
                        iNETUserID = CreateOnlineUser();
                    } catch (Exception e) {
                    }

..more code.. }

Here is the 'UsernameIsAvailable' function :

public boolean UsernameIsAvailable() {

    Boolean bUsernameIsAvailable = false;

    SoapObject request = new SoapObject(TA.sNAMESPACE,
            TA.METHOD_IsUsernameAvailable);
    request.addProperty("sUsername", sEmail);

    SoapSerializationEnvelo开发者_开发问答pe envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(
            TA.WEBSERVICES_URL);

    try {
        androidHttpTransport.call(TA.SOAP_ACTION_IsUsernameAvailable,
                envelope);
        SoapObject thisresponse = (SoapObject) envelope.bodyIn;

        for (int i = 0; i < thisresponse.getPropertyCount(); i++) {
            Object obj = thisresponse.getProperty(i);
            SoapPrimitive soapPrimitive = (SoapPrimitive) obj;
            bUsernameIsAvailable = Boolean.parseBoolean(soapPrimitive
                    .toString());
        }
    } catch (Exception e) {
    }

    return bUsernameIsAvailable;
}

I am using KSoap2 which is an external jar file to handle the web service stuff

Thanks Mike


You can structure your code in the following way:

private class MyBackgroundTask extends AsyncTask<Void, Void, Void> {
    protected void onPreExecute() {
        // show dialog
    }
    protected Void doInBackground(Void... unused) {
        // background processing
    }
    protected void onPostExecute(Void unused) {
        // dismiss dialog
        continueProcessing();
    }
}

private OnClickListener RegistrationListener = new OnClickListener() {
    public void onClick(View v) {
        // non-blocking code

        new MyBackgroundTask().execute();
    }
}

public void continueProcessing() {
    // the rest of the main activity
}


You should never ever ever block the main-ui thread. Just show a dialog with a message about what is going on. Make sure that the user can't close the dialog. As soon as the AsyncTask or runnable finishes its task just close the dialog.

Use Dialog setCancelable(false) to make sure the user can't close the dialog

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜