当使用我的应用程序连接到互联网,如果应用程序崩溃或被杀死后,我无法再次登录,我必须停止从应用程序->我的应用程序->停止按钮再次登录。
当跟随应用程序调试时,我的调试消息到达doInBackground方法的末尾,但我知道异步任务类在doInbackground方法之后调用onPostExecute,在我的示例中,在杀死或崩溃应用程序后,这是不做的。
有人能帮我解决这个问题吗?
private class doAuthentication extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
Requester req = new Requester();
String hash = req.loginByPIN(params[0]);//,VendingManagerActivity.this);
return hash;
}
@Override
protected void onPostExecute(String hash) {
Log.d("Vending","In post Ex-> "+hash);
// myProgressDialog.dismiss();
if (hash.equals("")) {
final EditText kbdInput = (EditText) findViewById(R.id.kbdInput);
kbdInput.setText("");
displayMessage(getString(R.string.err_wrong_credentials));
return;
} else if (hash.equals("-")) {
final EditText kbdInput = (EditText) findViewById(R.id.kbdInput);
kbdInput.setText("");
displayMessage(getString(R.string.err_no_permissions));
return;
} else {
String[] resp = hash.split("#");
Editor edit = mPrefs.edit();
edit.putString("login_hash",resp[0]);// hash);
edit.putString("password",resp[1]);//lbp.reset_pass);
edit.commit();
Intent mainScreenActivity = new Intent(
VendingManagerActivity.this, MainScreenActivity.class);
startActivity(mainScreenActivity);
finish();
}
}
}Requester类->方法loginByPIN ->
public String loginByPIN(String pin_code)
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("module", "tablet"));
nameValuePairs.add(new BasicNameValuePair("method", "loginByPin"));
nameValuePairs.add(new BasicNameValuePair("pin", pin_code));
String xmlData = getRequest(API_ADDRESS, nameValuePairs);
String hash = "";
String response = "";
try {
Serializer serializer = new Persister();
Reader reader = new StringReader(xmlData);
LoginByPin lbp =
serializer.read(LoginByPin.class, reader, false);
hash = lbp.hash;
Requester.HASH = hash;
Log.d("Vending", "HASH received: " + hash);
response = hash +"#"+lbp.reset_pass;
} catch (Exception e) {
Log.d("Vending",e.getMessage().toString());
Serializer serializer = new Persister();
Reader reader = new StringReader(xmlData);
try {
Error err =
serializer.read(Error.class, reader, false);
Integer errorCode = err.code;
String errorText = err.text;
Log.e("Vending", "ERROR: " + errorText + "(" + Integer.toString(errorCode) + ")");
if (err.code == 200)
return "-";
} catch (Exception e2) {
Log.e("Vending", "ERROR: Invalid output returned from API");
}
}
Log.d("Vending","Before returning-> "+hash);
return response;//hash;
}谢谢!
编辑:不以这种方式更改代码
@Override
protected String doInBackground(String... params) {
Requester req = new Requester();
String hash = req.loginByPIN(params[0]);//,VendingManagerActivity.this);
Log.d("Vending","Before doInBackground ends-> "+hash);
return hash;
}
@Override
protected void onPostExecute(String hash) {
Log.d("Vending","In post Ex-> "+hash);
myProgressDialog.dismiss();而logcat的输出是
10-29 10:26:06.278: D/Vending(25714): Before returning-> peq8qkjvee7v4nhci8v3dub293
10-29 10:26:06.278: D/Vending(25714): Before doInBackground ends-> peq8qkjvee7v4nhci8v3dub293#qwerty123发布于 2012-10-29 15:11:39
如果您的应用程序在doInBackground()执行时崩溃,则不会调用onPostExecute()。
只有在doInBackground()方法成功完成后才会调用onPostExecute()。
https://stackoverflow.com/questions/13117289
复制相似问题