I know that questions from ProgressDialog with Threads have been asked many times, but none of the solutions seem to work for my project. Basically, what I want to do is: 1) when the user clicks the button, the action sends an auth request to the server 2) while this is done, ProgressDialog is displayed 3) when the response comes, I want to reject the ProgressDialog and the returned object that should be read and interpreted by Activity.
If I: 1) set Thread to update the Application field with the answer, the next method (which is outside of Thread) gives NPE when accessing the field 2) if I include the next method in Thread, the second method throws "java.lang.RuntimeException: not manages to create a handler inside a thread that did not call Looper.prepare () "
Sorry for the long text, but I completely lose it for this ... My code is this:
public class XXX extends Activity implements OnClickListener {
private SoapObject returnObject;
private String response;
@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();
authenticateReal();
}
}).start();
mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 10:
authProgressDialog.dismiss();
break;
}
}
};
}
}
public void authenticate() {
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);
}
}
public void authenticateReal() {
}
source
share