问题描述
我只是没有经验的编程世界的新手。我即将完成我的Android项目,但我的问题是:即使我从我的应用程序输入了正确的用户名和密码,我的Toast消息总是显示LOGIN FAILED。请帮我正确的代码!! (我这样做了很长时间)
I am just new in programming world with no experience. I am about to finish my Android project but my problem is: My toast message always says LOGIN FAILED even if I entered the correct username and password from my application. Please help me with the correct code!! (i am doing this for a long time)
以下是我的登录活动:
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时,这是服务器的响应:
This is the response from server when I login the correct uname and pword from logcat:
11-15 09:50:58.811: D/request!(865): starting ...
11-15 09:51:02.041: D/Login attempt(865): {"success":"true"}
如果我记错了这就是日志uname或pword:
and this is the log if i log the wrong uname or 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,即使我的登录uname / pword是正确还是错误。
推荐答案
你没有返回服务器响应....返回null,所以onPostExecute的结果永远不会是成功。
You don't return the server response.... return null, so the onPostExecute's result never will be "success".
你应该返回json.getString(TAG_SUCCESS);
You should return json.getString(TAG_SUCCESS);
这篇关于Android:if / else在LoginActivity上遇到onPostExecute问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!