如何在Android中确认日志?

时间:2015-04-11 10:35:34

标签: android android-asynctask httpclient

我正在尝试编写一种可以登录在线数据库的方法。它工作正常,但当我写错了用户/密码时,我得到一条消息解释我必须把正确的用户/密码,如果我再次尝试错误的用户/密码,我得到"You are already logged in!"。我不知道如何管理inlogging? - 我希望代码告诉我,我没有登录并再试一次?! - 任何帮助都非常感谢 - 谢谢,史蒂夫。

有方法(我使用BroadcastReceiver来获取AsyncTask-onPosExecute()的回复):

public void logIn() {
        String user = edUser.getText().toString(), pwd = edPwd.getText().toString();

        if (httpClient == null) {
            httpClient = new DefaultHttpClient();
            List<NameValuePair> nameValuePairs = new ArrayList<>(2);
            nameValuePairs.add(new BasicNameValuePair("user", user));
            nameValuePairs.add(new BasicNameValuePair("Passord", pwd));

            new AsyncTaskLogIn(this).execute(new Pair<>(nameValuePairs, httpClient));
        } else {
            Toast.makeText(this, "You are already logged in!", Toast.LENGTH_LONG).show();
        }
        LocalBroadcastManager.getInstance(this).registerReceiver(LogIn,
                new IntentFilter("log_in"));
    }

    private BroadcastReceiver LogIn = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String result = intent.getStringExtra("myLogIn");
            tvMessage.setText(result);
        }            
    };

3 个答案:

答案 0 :(得分:0)

您的代码逻辑似乎不正确。改为:

if (httpClient == null) {
    httpClient = new DefaultHttpClient();
} 
List<NameValuePair> nameValuePairs = new ArrayList<>(2);
nameValuePairs.add(new BasicNameValuePair("user", user));
nameValuePairs.add(new BasicNameValuePair("Passord", pwd));
new AsyncTaskLogIn(this).execute(new Pair<>(nameValuePairs, httpClient));

这些代码确保httpClient不是null
PS:如果您想知道您是否成功登录,则需要覆盖onPostExecute的{​​{1}}。

答案 1 :(得分:0)

试试这个:这对你很有用。

    Login.setOnClickListener(new View.OnClickListener() {


            @SuppressWarnings("deprecation")
            public void onClick(View v) {
                isInternetPresent = cd.isConnectingToInternet();
             System.out.println("is internet present:::"+isInternetPresent);
                 String username=usrname.getText().toString();
                 String password=paswd.getText().toString();

                if(!username.equals("")&&!password.equals(""))
                {


                    if(isInternetPresent)
                    {
                        System.out.println(username);
                        System.out.println(password);
                        usernamepassed=username;
                        System.out.println("inside attempt login");
                        new AttemptLogin().execute();


                    }
                else
                {

                    System.out.println("No network connection.");


                }
                }
                else if(!password.equalsIgnoreCase(""))
                {

                    System.out.println("Please enter username.");


                }
                else if(!username.equalsIgnoreCase(""))
                {

                    System.out.println("Please enter password.");


                }
                else
                {

                    System.out.println("Enter login credentials.");


                  }
            }

});

异步任务::

 class AttemptLogin extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(LoginActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();

    }

    @Override
    protected String doInBackground(String... params) {

         List<NameValuePair> params1 = new ArrayList<NameValuePair>();

         params1.add(new BasicNameValuePair("username", usrname.getText().toString()));
         params1.add(new BasicNameValuePair("password", paswd.getText().toString()));

         JsonParser jLogin = new JsonParser();
         System.out.println(usrname.getText().toString());
         System.out.println( paswd.getText().toString());
         JSONObject json = jLogin.makeHttpRequest(loginurl,"POST", params1);
         System.out.println("value for json::"+json);
         if(json!=null)
         {
             try
             {
                 if(json != null)
                 {
                 System.out.println("json value::"+json);

                 JSONObject jUser = json.getJSONObject(TAG_SRESL);
                 successL = jUser.getString(TAG_SUCCESS1);
                 username1 = jUser.getString(TAG_USERNAME );
                 password1 = jUser.getString(TAG_PASSWORD);
                 orgid=jUser.getString(TAG_ORGID);
                 role=jUser.getString(TAG_ROLE);
                 enabled=jUser.getString(TAG_ENABLED);
                 System.out.println("username value:::"+username);
                 System.out.println("password value::"+password);
                 System.out.println("role value"+role);
                 Intent intentSignUP=new Intent(getApplicationContext(),DashboardActivity.class);
                    startActivity(intentSignUP);
                 }

            }

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

             }
          }
         else{

             successL ="No"; 
                  } 

            return null;
        }
    @SuppressWarnings("deprecation")
    @Override
     protected void onPostExecute(String file_url) {
           super.onPostExecute(file_url);
        System.out.println("in post execute");
           pDialog.dismiss();
          if(JsonParser.jss.equals("empty"))
          {
           System.out.println("json null value");

              System.out.println("Server not connected.");


                  pDialog.dismiss();
          }
          else if(successL.equalsIgnoreCase("No")){

                System.out.println("Invalid username or password.");


              pDialog.dismiss();
           }

     }



}

答案 2 :(得分:0)

1 - 将字符串"You are already logged in!"替换为"You aren't logged in!" 2 - 登录后,只需禁用/隐藏登录按钮即可 3 - 您还可以在登录时将全局布尔变量logged设置为true。

在按钮的点击处理程序中,如果已记录,则显示"You are already logged in!" 否则,请执行登录过程,将logged设置为true或false,具体取决于登录凭据是否匹配。