6
\$\begingroup\$

This code starts a new thread for login and reacts according to the JSON result returned by the server.

I think this code has too many conditionals, exception handlers, and nested functions.

public void Login() {
    mButton.setClickable(false);
    mButton.setText("Loading...");
    final String username = mUserName.getText().toString().trim();
    final String password = mPassWord.getText().toString().trim();

    new Thread() {
        public void run() {
            final String result = NetUtil.loginByGet(username, password);
            if (result != null) {
                try {
                    JSONTokener jsonTokener = new JSONTokener(result);
                    JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
                    final String message = jsonObject.getString("message");
                    if (jsonObject.getInt("error") == 0) {
                        String token = jsonObject.getString("token");
                        boolean isSaveSuccess = InfoUtil.saveUserInfo(LoginActivity.this,token);
                        if (isSaveSuccess) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                    initIntent(MainActivity.class);
                                    finish();
                                }
                            });
                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this, "保存登录信息失败", Toast.LENGTH_SHORT).show();
                                    mButton.setClickable(true);
                                    mButton.setText("登录");
                                }
                            });
                        }
                    } else if(jsonObject.getInt("error") == 1) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(LoginActivity.this, "" + message,
                                        Toast.LENGTH_SHORT).show();
                                mButton.setClickable(true);
                                mButton.setText("登录");
                            }
                        });
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(LoginActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                        mButton.setClickable(true);
                        mButton.setText("登录");
                    }
                });
            }
        }
    }.start();
}
\$\endgroup\$
2
  • \$\begingroup\$ Have you thought about writing a utility function (displayToast) for those repetitive runOnUiThread parts? \$\endgroup\$ Apr 27, 2016 at 13:48
  • \$\begingroup\$ I tried to make those function runOnUiThread But I don't know how to go by passing a parameter to distinguish between those Toast \$\endgroup\$
    – WangZhiYao
    Apr 27, 2016 at 13:55

1 Answer 1

7
\$\begingroup\$

Using an AsyncTask instead would be much better. You've got many callbacks to the UI thread, all of this you could do in the post execute method instead:

private class LoginTask extends AsyncTask<Void, Void, String>{

    private String username;
    private String password;
    private Exception exception;
    private boolean isSaveSuccess;

    public LoginTask(String username, String password){
        this.username = username;
        this.password = password;
    }

    @Override
    protected String doInBackground(Void... voids) {
        try{
            String result = NetUtil.loginByGet(username, password);
            //Grab the token
            isSaveSuccess = InfoUtil.saveUserInfo(LoginActivity.this,token);
        }catch(Exception ex){
            //Catch any given exception to use it in the post execution (e.g. show a toast)
            this.exception = ex;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        //Here it goes your UI logic
    }

} 

To call it, just launch an execution when login button is clicked:

public void doLogin(){    
    mButton.setClickable(false);
    mButton.setText("Loading...");
    String username = mUserName.getText().toString().trim();
    String password = mPassWord.getText().toString().trim();
    new LoginTask(username, password).execute();
}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.