android - 如何在 Activity 从 Web 服务请求 SoapObject 时实现 ProgressDialog?

标签 android multithreading android-activity progressdialog

我知道 ProgressDialog with Threads 问题已被问过很多次,但似乎没有一个解决方案适用于我的项目。基本上我想做的是: 1)当用户点击一个按钮时, Activity 向服务器发送一个授权请求 2) 执行此操作时会显示 ProgressDialog 3) 当响应到来时,我想关闭 ProgressDialog 和要由 Activity 读取和解释的返回对象

如果我: 1) 设置 Thread 以使用响应更新 Application 字段,下一个方法(在 Thread 之外)在访问该字段时抛出 NPE 2) 如果我在 Thread 中包含下一个方法,第二个方法将抛出“java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序”

抱歉,文字很长,但我完全失去了它......我的代码是这样的:

public class XXX extends Activity implements OnClickListener {

// (...)
private SoapObject returnObject;
private String response;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // (...)
        authProgressDialog = ProgressDialog.show(XXX.this, "", "Authenticating...", true, false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                authenticate(); // method that calls the API via SOAP
                authenticateReal(); // method that handles the response
            }
        }).start();

        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 10:
                        authProgressDialog.dismiss();
                        break;
                }
            }
        };
    }
}

public void authenticate() {
    // API stuff (...)
    AndroidHttpTransport aht = new AndroidHttpTransport(URL);
    try {
        aht.call(SOAP_ACTION, soapEnvelope);
        returnObject = (SoapObject) soapEnvelope.getResponse();
        response = returnObject.getProperty("ResponseStatus").toString();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        mHandler.sendEmptyMessage(10);
    }
}

// Method that needs to access returnObject and reponse objects and
// it is here where the NPE's or other exceptions are thrown
public void authenticateReal() {
// (...)
}

最佳答案

你最好使用AsyncTask (这是 Android 的方式):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new TheTask().execute();
}

private class TheTask extends AsyncTask<Void, Void, Void>{

    @Override
    protected void onPreExecute() {
        authProgressDialog = ProgressDialog.show(XXX.this, "", "Authenticating...", true, false);
    }

    @Override
    protected Void doInBackground(Void... params) {
        authenticate(); // method that calls the API via SOAP
        authenticateReal(); // method that handles the response
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        authProgressDialog.dismiss();
    }
}

顺便说一下...我发现这个演示文稿非常有用(它讨论 REST 应用程序,但您可以将相同的概念应用于不同类型的应用程序):Developing Android REST client applications

关于android - 如何在 Activity 从 Web 服务请求 SoapObject 时实现 ProgressDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4776236/

相关文章:

java - Android 上 java.awt.image.BufferedImage 的 ClassNotFoundException

java - 如何解决 "No activity found to handle intent"异常?

Android - 重新启动 Activity 而无需重新创建它

java - 无法将文本附加到 Google 云端硬盘文件中

安卓;类型 com.google.gson.ExclusionStrategy 被定义多次 :

java - 如何从 JSON 文本中获取 url

c# - 异步更新 ObservableCollection<T> 会导致挂起,并且不会更新 GUI

java - 除非重新启动应用程序,否则某些线程将始终处于“等待”状态

java - 如何根据 firebase 中的 id 更新另一个用户余额?

android - Json 数据已发布到服务器但始终获取 "com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of"