如何在执行asynctask之前添加客户端验证?

时间:2014-11-22 07:14:21

标签: android validation login

您好我是Android的新手,在我的应用程序中我有登录页面,它与我的回复工作正常,但我想添加这样的东西,如果我的用户名和密码edittext是空白的,用户点击登录按钮它应该显示消息而不是启动异步任务。

公共类LoginPage extends Activity实现OnClickListener {

private Button btn;
private EditText user;
private EditText pass;


// Alert Dialog Manager
    AlertDialogManager alert = new AlertDialogManager();

    // Session Manager Class
    SessionManager session;

// Progress Dialog
private ProgressDialog pDialog;
//JSON parser class
JSONParser jsonParser = new JSONParser();
private Button btn1;
private String userid;

private static final String LOGIN_URL = "xxxxx";


private static final String TAG_SUCCESS = "status";
private static final String TAG_LOGIN = "login";
private static String TAG_USERID="user_login_id";
private static final String TAG_SESSION="session";
String session_id="";


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_page);

    // Session Manager
    session = new SessionManager(getApplicationContext());

    user=(EditText)findViewById(R.id.loginmailid);
    pass=(EditText)findViewById(R.id.loginpwd);

    Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();

    btn=(Button)findViewById(R.id.login);
    btn1=(Button)findViewById(R.id.btnreg);
    btn.setOnClickListener(this);



}
@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.login:
        if(user.equals(null) || pass.equals(null))
        {
            Toast.makeText(getApplicationContext(), "Enter Email and Password", Toast.LENGTH_SHORT).show();
        }
        else
        {
        new AttemptLogin().execute();
        System.out.println("<<<<<<<<<<<<<<<<<<<<< Session ID : " + session_id);
        session.createLoginSession(session_id);
        System.out.println(session);
        break;
        }

    case R.id.btnreg:
        Intent i = new Intent(this, RegistrationForm.class);
        startActivity(i);
        break;

    default:
            break;
    }

}

类AttemptLogin扩展了AsyncTask {

    boolean failure = false;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(LoginPage.this);
        pDialog.setMessage("Login..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @SuppressWarnings("unused")
    @Override
    protected String doInBackground(String...args) {
        //Check for success tag
        //int success;
        Looper.prepare();
        String username = user.getText().toString();
        String password = pass.getText().toString();
         try {
             //Building Parameters


             List<NameValuePair> params = new ArrayList<NameValuePair>();
             params.add(new BasicNameValuePair("email", username));
             params.add(new BasicNameValuePair("password", password));
             params.add(new BasicNameValuePair("version", "apps"));

             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());

             JSONObject jobj = new JSONObject(json.toString());
             final String msg = jobj.getString("msg");
             session_id = jobj.getString("user_login_id");
             System.out.println("Session ID : " + session_id);
             System.out.println("MSG : " + msg);

             runOnUiThread(new  Runnable() 
             {
                @Override
                public void run() 
                {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
                } 
            });
             return json.getString(TAG_SUCCESS);


             //JSONArray arr = json.getJSONArray("login");

            //System.out.println(arr.toString());
            //JSONObject arr1  = new JSONObject(json);
            //String ss=arr1.getString("status");
            //System.out.println(ss);
            //System.out.println(arr1.getString("status"));
             //String date = jObj.getString("status");



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

    // After completing background task Dismiss the progress dialog

    protected void onPostExecute(String file_url) {
        //dismiss the dialog once product deleted


        pDialog.dismiss();
         if(file_url.equals("success")) {

                 session.createLoginSession(session_id);
                // Log.d("Login Successful!", json.toString());
                 Intent i = new Intent(LoginPage.this, MainActivity.class);
                 i.putExtra("id", session_id);
                 System.out.println("Session Id : >>>>>>>>>>>>>>>>" + session_id);
                 startActivity(i);

             }else{
                 //Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
             }
}}

}

2 个答案:

答案 0 :(得分:0)

尝试这样,

 if(user.getText().toString().trim().length() > 0 && pass.getText().toString().trim().length() > 0)
        {
        new AttemptLogin().execute();
        System.out.println("<<<<<<<<<<<<<<<<<<<<< Session ID : " + session_id);
        session.createLoginSession(session_id);
        System.out.println(session);
        break;
        }
        else
        {
       Toast.makeText(getApplicationContext(), "Enter Email and Password", Toast.LENGTH_SHORT).show();
        }

答案 1 :(得分:0)

以这种方式试试......

case R.id.login:
        if(user.getText().toString().trim().length()==0 || pass.getText().toString().trim().length()==0)
        {
            Toast.makeText(getApplicationContext(), "Enter Email and Password", Toast.LENGTH_SHORT).show();
        }
        else
        {
        new AttemptLogin().execute();
        System.out.println("<<<<<<<<<<<<<<<<<<<<< Session ID : " + session_id);
        session.createLoginSession(session_id);
        System.out.println(session);
        break;
        }