Android onPostExecute()未在AsyncTask中执行
我正在尝试创建一个活动,允许用户通过检查数据库中的用户名和密码登录,但是在成功获取凭据后,doInbackground不会停止执行。我不确定如何使onpostexecute运行。这是密码Android onPostExecute()未在AsyncTask中执行,android,Android,我正在尝试创建一个活动,允许用户通过检查数据库中的用户名和密码登录,但是在成功获取凭据后,doInbackground不会停止执行。我不确定如何使onpostexecute运行。这是密码 public class LoginActivity extends Activity{ public String username; public String password; public String userid; JSONParser jParser = new JSONParser();
public class LoginActivity extends Activity{
public String username;
public String password;
public String userid;
JSONParser jParser = new JSONParser();
JSONObject json;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
context=getApplicationContext();
setContentView(R.layout.activity_login);
Button loginbutton=(Button) findViewById(R.id.loginbutton);
final EditText usernameText=(EditText) findViewById(R.id.usernameInput);
final EditText passwordText=(EditText) findViewById(R.id.passwordInput);
loginbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
username=usernameText.getText().toString();
password=passwordText.getText().toString();
if(username.trim().length()==0 || password.trim().length()==0){
AlertDialogManager diag=new AlertDialogManager();
diag.showAlertDialog(getApplicationContext(), "Fill Fields", "enter a username and password", false);
}else{
//send the username and password for verification
new Login().execute();
}
}
});
}
//http class starts here.
class Login extends AsyncTask<String, String, String> {
InputStream is = null;
JSONObject jObj = null;
ProgressDialog pDialog;
static final String url = "http://10.0.2.2/newptk/dalvik/auth.php";
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Authenticating...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
Log.e("Auth", "working");
JSONArray document = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
json = jParser.makeHttpRequest(url, "POST", params);
return null;
}
protected void onPostExecute() {
pDialog.dismiss();
SessionManager smg=new SessionManager(getApplicationContext());
int flag = 0;
try {
flag = json.getInt("success");
if(flag==1){
userid=json.getString("userid");
//set the session
smg.createLoginSession(username, userid);
//Login the user
Intent i = new Intent(getApplicationContext(), ReportFound.class);
startActivity(i);
finish();
}else{
AlertDialogManager diag=new AlertDialogManager();
diag.showAlertDialog(LoginActivity.this, "Login", "Incorrect Username/password", false);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}//end of http class
}我看到的第一件事是,您告诉onPostExecute在类头中接受一个字符串
class Login extends AsyncTask<String, String, String>
如果您不想传递任何内容,请将其更改为
class Login extends AsyncTask<Void, Void, Void>
请注意
异步任务使用的三种类型如下:
Params,执行时发送到任务的参数类型
Progress,在过程中发布的进度单位的类型
背景计算
Result,结果的类型
背景计算
异步任务并不总是使用所有类型。要将类型标记为未使用,只需使用类型Void:
改变
protected void onPostExecute()
到
你应该是金色的。考虑在代码中添加@超越标记,以防止将来出现这些微妙的错误。您是否尝试调试代码以查看doInEnror停止的位置?它看起来不象是在重写适当的OnPrExcess。您刚刚定义了一个不会被调用的。尝试对onPostExecute使用自动完成。无论参数类型是什么,都将调用onPostExecute。。。正确吗?@AlexLockwood如果参数与类头和方法头中的内容不匹配,则不会这样做……我不相信。如果它确实被调用,那么将会有错误/exceptions@AlexLockwood如果您没有重写AsyncTask类的onPostExecute方法,那么您只是自己创建了一个不会被调用的实例。这就是他试图指出的一个可能的错误。@AlexLockwood在doinbackground完成onPostExecute时被调用。除非你有一个崩溃是的,它被调用。是的,我想指出缺少的覆盖,但我试图找出我的答案编辑
protected void onPostExecute(Void result) {
...
}
@Override
protected void doInBackground(String... arg0) {
private class MyTask extends AsyncTask<Void, Void, Void> { ... }
protected void onPostExecute()
protected void onPostExecute(String result)