java - 我的第一次登录尝试没有给我任何消息

标签 java android forms authentication http-post

第一次尝试登录,无论登录信息是否正确,当它应该说“用户ID/密码不正确”或“登录成功!”时,我没有得到任何回复。但在第一次尝试后的任何时候它都会打印正确的消息,为什么要这样做?

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);
    un = (EditText) findViewById(R.id.et_un);
    pw = (EditText) findViewById(R.id.et_pw);
    ok = (Button) findViewById(R.id.btn_login);
//  error = (TextView) findViewById(R.id.tv_error);
    textview = (TextView) findViewById(R.id.textView1);

    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            /**
             * According with the new StrictGuard policy, running long tasks
             * on the Main UI thread is not possible So creating new thread
             * to create and execute http operations
             */
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                    postParameters.add(new BasicNameValuePair("UserID",
                            un.getText().toString()));
                    postParameters.add(new BasicNameValuePair("Password",
                            pw.getText().toString()));

                    String response = null;
                    try {
                        response = SimpleHttpClient
                                .executeHttpPost("website",
                                        postParameters);
                         res = response.toString();

                        resp = res.replaceAll("\\s+", "");
                        System.out.println(res);


                    } catch (Exception e) {
                        e.printStackTrace();
                        errorMsg = e.getMessage();
                    }
                }
            }).start();
            try {
                Thread.sleep(1000);

                /**
                 * Inside the new thread we cannot update the main thread So
                 * updating the main thread outside the new thread
                 */

                //error.setText(resp);
                if(res.indexOf("<TITLE>LoginOk</TITLE>") != -1  ||(resp.indexOf("<TITLE>Login</TITLE>") != -1)) {                   
                    textview.setText("Incorrect User ID / Password");


                }
                else if(res.indexOf("Already") != -1){

                textview.setText("An account is already logged In");
                }
                else{
                textview.setText("Login Successful! ");
                }
                if (null != errorMsg && !errorMsg.isEmpty()) {
                    textview.setText(errorMsg);
                }
            } catch (Exception e) {
                textview.setText(e.getMessage());
            }
        }
    });
}

最佳答案

在您的代码中,您放置的用于显示消息的条件在线程运行之前执行,因此您的响应始终为空或空白,这不满足您的任何条件,因此无法正确执行。

您需要做的是,在主代码实现之后,简单地实现您需要在线程中调用的处理程序。

                final Handler handler = new Handler(){

                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);

                      /**
                     * Inside the new thread we cannot update the main thread So
                     * updating the main thread outside the new thread
                     */

                    //error.setText(resp);
                    if(res.indexOf("<TITLE>LoginOk</TITLE>") != -1  ||(resp.indexOf("<TITLE>Login</TITLE>") != -1)) {                   
                        textview.setText("Incorrect User ID / Password");


                    }
                    else if(res.indexOf("Already") != -1){

                    textview.setText("An account is already logged In");
                    }
                    else{
                    textview.setText("Login Successful! ");
                    }
                    if (null != errorMsg && !errorMsg.isEmpty()) {
                        textview.setText(errorMsg);
                    }
                }

            };


            Thread thread = new Thread(){

                @Override
                public void run() {
                    super.run();


                    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                    postParameters.add(new BasicNameValuePair("UserID",
                            un.getText().toString()));
                    postParameters.add(new BasicNameValuePair("Password",
                            pw.getText().toString()));

                    String response = null;
                    try {
                        response = SimpleHttpClient
                                .executeHttpPost("http://www.e-learning.com/wlc_loginok.asp",
                                        postParameters);
                         res = response.toString();

                        resp = res.replaceAll("\\s+", "");
                        System.out.println(res);


                    } catch (Exception e) {
                        e.printStackTrace();
                        errorMsg = e.getMessage();
                    }finally{
                        handler.sendEmptyMessage(0);
                    }

                }

            };
            thread.start();

关于java - 我的第一次登录尝试没有给我任何消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24255679/

相关文章:

android - android 中的 XMPPConnection 错误

Android服务启动10秒后被杀死

android - tablayout 中的 recyclerview 只被调用一次

python - web.py中的表单验证器,帮助查找错误

jquery - 从 AJAX 的许多页面中序列化独特的表单。适用于 FF/Safari,但 IE 会序列化页面上的所有表单

java - mvn -version 正在显示 -bash :/usr/local/bin/mvn: No such file or directory. 知道我该如何解决这个问题吗?

java - 无法在fragment中实现Recycler View 适配器

java - 缓冲阅读器 HTTP POST

java - 在等待退出信号时处理InterruptedException(Android中的错误?)

c# - 将我的项目中的表单添加到另一个项目