带有远程服务器登录的Android对话框

时间:2011-10-06 06:23:57

标签: android httpclient android-alertdialog

以下是Android中AlertDialog的一点初始化以及使用HttpClient验证用户的方法

private void loginBox() {
    USERS = db.getUserData();

    login_layout = (LinearLayout) findViewById(R.id.loginlayout);
    loginLayout = (LinearLayout) View.inflate(this, R.layout.login, null);
    usernameEditText = (EditText) loginLayout.findViewById(R.id.username);
    passwordEditText = (EditText) loginLayout.findViewById(R.id.password);
    rememmber = (CheckBox) loginLayout.findViewById(R.id.checkBox1);
    gallery = (Gallery) loginLayout.findViewById(R.id.users_gallery);
    notificationText = (TextView) loginLayout
            .findViewById(R.id.notificationtext);
    userChooseText = (TextView) loginLayout
            .findViewById(R.id.user_choose_text);
    breakLine = (View) loginLayout.findViewById(R.id.login_break_line);

    registerForContextMenu(loginLayout);

    galleryVisibility();

    gallery.setAdapter(new AddImgAdp(this));

    if (userPrefs.getUserName() == null
            && userPrefs.getUserPassword() == null) {

        if (!USERS.isEmpty()) {

            System.out.println("not empty");
            usernameEditText.setText(
                    USERS.get(gallery.getSelectedItemPosition())
                            .getUsername(), TextView.BufferType.EDITABLE);
            passwordEditText.setText(
                    USERS.get(gallery.getSelectedItemPosition())
                            .getPassword(), TextView.BufferType.EDITABLE);
        }
    }

    gallery.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            selectedUser = USERS.get(arg2).getUsername();

            if (!USERS.isEmpty()) {
                System.out.println("not empty");
                usernameEditText.setText(USERS.get(arg2).getUsername(),
                        TextView.BufferType.EDITABLE);
                passwordEditText.setText(USERS.get(arg2).getPassword(),
                        TextView.BufferType.EDITABLE);
            }

            return false;
        }
    });

    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            usernameEditText.setText(USERS.get(position).getUsername(),
                    TextView.BufferType.EDITABLE);
            passwordEditText.setText(USERS.get(position).getPassword(),
                    TextView.BufferType.EDITABLE);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    // rememmber.setOnClickListener(new View.OnClickListener() {
    //
    // @Override
    // public void onClick(View v) {
    // if (rememmber.isChecked()) {
    // Toast toast = Toast.makeText(getApplicationContext(),
    // "Try to avoid this option, it is unsecure!",
    // Toast.LENGTH_LONG);
    // toast.show();
    //
    // // TODO: store in DB
    // }
    // }
    // });

    Button loginButton = (Button) loginLayout
            .findViewById(R.id.action_login);
    loginButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            username = usernameEditText.getText().toString();

            password = passwordEditText.getText().toString();

            if (rememmber.isChecked()) {
                db.createNewUser(username, password);
            }
            if (testUser()) {
                // gallery.removeAllViewsInLayout();
                username = usernameEditText.getText().toString();

                password = passwordEditText.getText().toString();

                userPrefs.savePrefs(username, password);

                alert.dismiss();
                // alert.cancel();
                // login = null;
                // alert = null;
            } else {
                userPrefs.clearPrefs();

                System.out.println("Gallery children: "
                        + gallery.getChildCount());

                notificationText.setText("Username or password incorrect!");
                notificationText.setTextColor(Color.RED);

                USERS = db.getUserData();

                galleryVisibility();

                ((BaseAdapter) gallery.getAdapter()).notifyDataSetChanged();

                gallery.setSelection(gallery.getCount());
            }

        }
    });

    if (!initialized) {
        System.out.println("Run login box initialization");

        initialized = true;
        login = new AlertDialog.Builder(this);
        login.setView(loginLayout).setTitle("Logon Credentials")
                .setCancelable(false);

        alert = login.create();
        alert.show();
    }
}

private void galleryVisibility() {
    if (USERS.size() < 1) {
        gallery.setEnabled(false);
        gallery.setVisibility(LinearLayout.INVISIBLE);
        breakLine.setEnabled(false);
        breakLine.setVisibility(LinearLayout.INVISIBLE);
        userChooseText.setEnabled(false);
        userChooseText.setVisibility(LinearLayout.INVISIBLE);
    } else {
        gallery.setEnabled(true);
        gallery.setVisibility(LinearLayout.VISIBLE);
        breakLine.setEnabled(true);
        breakLine.setVisibility(LinearLayout.VISIBLE);
        userChooseText.setEnabled(true);
        userChooseText.setVisibility(LinearLayout.VISIBLE);
    }
}

private boolean testUser() {
        System.out.println("Testing user");

        if (usernameEditText.getText().toString()!= null
                && , passwordEditText.getText().toString() != null) {

            try {
                System.out.println("getting viewwstate");
                String viewState = client
                        .httpGetViewstate("http://website/Login.aspx");

                HttpResponse response = client.httpPost1(viewState,
                        "http://website/Login.aspx",
                        usernameEditText.getText().toString(), passwordEditText
                            .getText().toString());
                System.out.println("posted!");

                HttpEntity entity = response.getEntity();
                String responseText = EntityUtils.toString(entity);
                System.out.println(responseText);

                // String commentsHtml = client
                // .httpGet("http://website/PriceTables.aspx");

                // System.out.println(commentsHtml);

                if (responseText.contains("Log Out")) {

                    success = true;
                    System.out.println("username is ok");

                    this.finish();

                    return true;
                } else {
                    System.out.println("back again..");
                    fail = true;
                    // loginBox();
                    return false;
                }
            } catch (SocketTimeoutException socket) {
                System.out.println("login socket timeout");
            } catch (IOException e) {
                System.out.println("Login io exception");
            }
            return false;
        } else {
            // Intent login = new Intent(getApplicationContext(),
            // Login.class);
            // startActivity(login);
            fail = true;
            // loginBox();
            return false;
        }
    }

问题是当我第一次点击登录按钮时,一切正常,但是当我第二次点击它时,testUserMethod,尝试{语句被忽略..发现问题 - 再次,我自己的错误,现在一切都好。谢谢所有帮助过的人

2 个答案:

答案 0 :(得分:0)

您可以使用setCancelable()进行检查..您将其设置为true一次又一次为false ...

答案 1 :(得分:0)

看起来问题不在这里。也许,你做了一些静态的场景?