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

Tom*_*ala 3 multithreading android progressdialog android-activity

我知道带有Threads问题的ProgressDialog已被多次询问,但没有一个解决方案似乎适用于我的项目.基本上我想要做的是:1)当用户点击一个按钮时,Activity向服务器发送一个auth请求2)在完成此操作时会显示ProgressDialog 3)当响应出现时我想要关闭ProgressDialog和Activity要读取和解释的返回对象

如果I: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() {
// (...)
}
Run Code Online (Sandbox Code Playgroud)

Cri*_*ian 8

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

顺便说一下......我发现这个演示文稿非常有用(它讨论了REST应用程序,但你可以为不同类型的应用程序应用相同的概念):开发Android REST客户端应用程序