0

我只是编程世界的新手,没有经验。我即将完成我的 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”,即使我的登录名/密码是正确或错误的。
4

3 回答 3

1

你在你的返回 null doInBackground,因此你总是登录失败。您需要String根据登录的成功/失败传递一些信息。

于 2013-11-13T07:47:38.220 回答
0

你返回 null doInbackground。的结果doInbackground是一个参数onPostExecute。您需要返回一个String.

http://developer.android.com/reference/android/os/AsyncTask.html

改成

 String success =json.getString(TAG_SUCCESS);

            } catch (JSONException e) {
                e.printStackTrace();
            }

 return sucess;

也改变

case R.id.btn_login:
            if (isInternetPresent) {
                new AttemptLogin().execute(); 
            }else {
            Toast.makeText(LoginActivity.this, "NO NETWORK CONNECTION", Toast.LENGTH_LONG).show();
            }
 break; 

编辑 :

    @Override
        protected String doInBackground(String... args) {
            String username = user.getText().toString();
            String password = pass.getText().toString();
            String success =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());
                success = json.getString("success");

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return success;
        }
于 2013-11-13T07:48:28.657 回答
0

您不返回服务器响应....返回 null,因此 onPostExecute 的结果永远不会是“成功”。

你应该返回 json.getString(TAG_SUCCESS);

于 2013-11-13T07:49:00.273 回答