我只是编程世界的新手,没有经验。我即将完成我的 Android 项目,但我的问题是:即使我从应用程序中输入了正确的用户名和密码,我的 toast 消息也总是显示 LOGIN FAILED。请帮助我正确的代码!(我这样做了很长时间)
以下是我的登录活动:
class AttemptLogin extends AsyncTask<String, String, String> {
//three methods get called, first preExecture, then do in background, and once do
//in back ground is completed, the onPost execute method will be called.
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
String username = user.getText().toString();
String password = pass.getText().toString();
//int success;
//String message =null;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uname", username));
params.add(new BasicNameValuePair("pword", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
return json.getString(TAG_SUCCESS);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String result) {
super.onPostExecute(result);
//JSONObject json;
// dismiss the dialog once product deleted
pDialog.dismiss();
//if (TAG_SUCCESS == null) {
if (result.equals("success")) {
//if (success.equals(success)) {
//Log.d("Login Successful!", json.toString());
Toast.makeText(LoginActivity.this, "Login Successful!", Toast.LENGTH_SHORT).show();
//Toast.makeText(LoginActivity.this, "Login Successful!", Toast.LENGTH_LONG).show();
//Intent i = new Intent(LoginActivity.this, PortalContents.class);
finish();
//startActivity(i);
//return success;
}
else {
//Log.d("Login Failure!", json.getString(TAG_MESSAGE));
Toast.makeText(LoginActivity.this, "Login Fail!", Toast.LENGTH_LONG).show();
//return json.getString(TAG_MESSAGE);
//return json.getString(TAG_MESSAGE);
}
}
}
}
这是我从 logcat 登录正确的 uname 和 pword 时服务器的响应:
11-15 09:50:58.811: D/request!(865): starting ...
11-15 09:51:02.041: D/Login attempt(865): {"success":"true"}
如果我记录了错误的 uname 或 pword,这就是日志:
11-15 09:57:23.352: D/request!(865): starting ...
11-15 09:57:25.982: D/Login attempt(865): {"message":"Invalid username or password","success":"failed"}
- 看起来服务器响应正确。但是,来自 onPostExecute 的消息 toast 始终响应为“LOGIN FAIL”,即使我的登录名/密码是正确或错误的。