显示“请稍候”在网络服务调用期间

发布于 2024-11-30 01:20:27 字数 1835 浏览 0 评论 0原文

我有一个通过 .net Web 服务检查用户名可用性的活动,我需要在通话期间显示“请等待”消息,因为它可能需要一两秒钟 - 我已经尝试过 AsyncTask 和 Threading,但我遇到的问题是我实际上想要阻塞主(也是唯一)线程,直到 Web 服务调用完成,因为我依赖它的结果来处理其他逻辑。

我已经尝试了各种方法,但没有任何乐趣 - 有人可以帮忙吗?

根据要求,这是我的代码的一部分:

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

..更多代码.. 这

是“UsernameIsAvailable”函数:

public boolean UsernameIsAvailable() {

    Boolean bUsernameIsAvailable = false;

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

    SoapSerializationEnvelope 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;
}

我正在使用 KSoap2,它是一个外部 jar 文件来处理 Web 服务内容,

谢谢 麦克风

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

    SoapSerializationEnvelope 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放肆 2024-12-07 01:20:27

您可以按以下方式构建代码:

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 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
}
渡你暖光 2024-12-07 01:20:27

你永远不应该阻塞 main-ui 线程。只需显示一个对话框,其中包含有关正在发生的情况的消息。确保用户无法关闭对话框。一旦 AsyncTask 或 runnable 完成其任务,只需关闭对话框即可。

使用 对话框 setCancelable(false) 以确保用户无法关闭对话框

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文