-1

我正在尝试创建一个允许用户通过检查数据库中的用户名和密码来登录的活动,但是在成功获取凭据后,doInbackground 不会停止执行。我不确定我能做些什么来让 onpostexecute 运行. 这是代码

 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

}

4

2 回答 2

1

我看到的第一件事是你告诉在你的班级标题onPostExecute()中接受 aString

class Login extends AsyncTask<String, String, String> 

但不接受任何东西或传递任何东西给它

protected void onPostExecute() {

如果您不想传递任何内容,请将其更改为

class Login extends AsyncTask<Void, Void, Void> 

protected void onPostExecute(Void result) {
...
}

@Override
protected void doInBackground(String... arg0) {

请注意Docs中的此部分

异步任务使用的三种类型如下:

Params,执行时发送给任务的参数类型。

进度,在后台计算期间发布的进度单元的类型。

Result,后台计算结果的类型。

并非所有类型都始终由异步任务使用。要将类型标记为未使用,只需使用类型 Void:

 private class MyTask extends AsyncTask<Void, Void, Void> { ... }
于 2013-05-23T19:21:35.020 回答
1

改变

protected void onPostExecute()

protected void onPostExecute(String result)

你应该是金色的。考虑@Override在您的代码中添加标签以防止将来出现这些细微的错误。

于 2013-05-23T19:40:50.420 回答