-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()a を受け入れるように言っていることですString

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) {

ドキュメントのこのセクションに注意してください

非同期タスクで使用される 3 つのタイプは次のとおりです。

Params、実行時にタスクに送信されるパラメーターのタイプ。

進行状況、バックグラウンド計算中に発行された進行状況単位のタイプ。

結果、バックグラウンド計算の結果の型。

すべての型が常に非同期タスクで使用されるわけではありません。タイプを未使用としてマークするには、単にタイプ Void を使用します。

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