如何使用AsyncTask类检查用户名和密码是否正确

时间:2014-04-03 14:04:55

标签: android android-asynctask passwords username

这是我第一次使用AsyncTask类 我有一个简单的登录活动,使用asyncTask类来连接外部数据库所以我需要检查用户名和密码是否正确做一些操作,这个检查必须在我认为的onPostExecute方法中完成。

所以怎么做这个检查谁能帮帮我?

MainActivity.java

类登录扩展了AsyncTask {

/**
 * Before starting background thread Show Progress Dialog
 * */
@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setMessage("Loading...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}

/**
 * Creating product
 * */
protected String doInBackground(String... args) {

    String user = username_edt.getText().toString();
    String password = pass_edt.getText().toString();
    // String description = inputDesc.getText().toString();

    String result = user.toString() + password.toString();

    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("user", user));
    params.add(new BasicNameValuePair("password", password));
    // params.add(new BasicNameValuePair("description", description));

    // getting JSON Object
    // Note that create product url accepts POST method
    JSONObject json = jsonParser.makeHttpRequest(url_login_user,
            "POST", params);

    // check log cat from response
    // Log.d("Create Response", json.toString());

    // check for success tag
    try {
        int success = json.getInt(TAG_SUCCESS);

        if (success == 1) {

            Intent in = new Intent(getBaseContext(), NewActivity.class);
            in.putExtra("ShowResult", result);
            startActivity(in);

        } else {
            // failed
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}

/**
 * After completing background task Dismiss the progress dialog
 * **/
protected void onPostExecute(String result) {
    // dismiss the dialog once done
    pDialog.dismiss();

}

}

1 个答案:

答案 0 :(得分:0)

我通过在MainActivity和doInBackground方法 json.getString(&#34; Tag_message&#34;)中添加 TAG FOR MESSAGE 来解决这个问题在onPostExecute中显示将包含从服务器端返回的json响应的结果。