Login page Android

   loginButton.setOnClickListener(new View.OnClickListener() {
        @Override        public void onClick(View view) {

            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            if (cm.getActiveNetworkInfo() != null) {

                el_uname = edt1_uname.getText().toString();
                el_pass = edt1_pass.getText().toString();

                if (edt1_uname.length() > 0 && edt1_pass.length() > 0) {

                    login_user();

                } else {
                    if (el_uname.length() == 0) {
                        edt1_uname.requestFocus();
                        edt1_uname.setError("Enter User Name");
                    } else if (el_pass.length() < 5) {
                        edt1_pass.requestFocus();
                        edt1_pass.setError("Enter Password Must Be 5 char");
                    }

                }

            } else {
                Toast.makeText(getApplicationContext(), "Turn On Your Internet Connection", Toast.LENGTH_SHORT).show();
            }


        }
    });
}

public void login_user() {

    final ProgressDialog loading = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
    loading.setMessage("Loading");
    loading.show();
    loading.setCancelable(true);
    loading.setCanceledOnTouchOutside(false);

    RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(BASE_URL) //Setting the Root URL            .build();

    AppConfig.login_user api = adapter.create(AppConfig.login_user.class);
    api.insertData(

            edt1_uname.getText().toString(),
            edt1_pass.getText().toString(),


            new Callback<Response>() {
                @Override                public void success(Response result, Response response) {

                    try {

                        BufferedReader reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
                        String resp;
                        resp = reader.readLine();
                        Log.d("success", "" + resp);

                        JSONObject jObj = new JSONObject(resp);
                        int success = jObj.getInt("success");

                        if (success == 1) {

                            JSONArray j1 = jObj.getJSONArray("result"); //array object create thay te

                            for (int i = 0; i < j1.length(); i++) {
                                JSONObject obj = j1.getJSONObject(i);

                                user_id = obj.getString("user_id");
                                user_name = obj.getString("user_name");
                              }

                            loading.dismiss();

                            LoginPreference loginpreference = new LoginPreference(getApplicationContext());
                            loginpreference.set_login_value(user_id, user_name);

                            Intent in = new Intent(getApplicationContext(), Home.class);
                            in.putExtra("u_id", user_id);
                            in.putExtra("u_name", user_name);

                            startActivity(in);
                            finish();

                        } else {
                            loading.dismiss();
                            Toast.makeText(getApplicationContext(), "Your Username Or Password Is Invalid", Toast.LENGTH_SHORT).show();
                        }

                    } catch (IOException e) {
                        Log.d("Exception", e.toString());
                    } catch (JSONException e) {
                        Log.d("JsonException", e.toString());
                    }
                }

                @Override                public void failure(RetrofitError error) {

                    Toast.makeText(MainActivity.this, "Check Your Internet Connection", Toast.LENGTH_LONG).show();
                }
            }

    );
}

=================

public interface login_user {
    @FormUrlEncoded    @POST("/status/user_login.php")
    void insertData(
            @Field("user_name") String edt_username,
            @Field("user_password") String edt_password,
            Callback<Response> callback);
}
 
 
 
 
====================Preference class=======================
 
public class LoginPreference {
    SharedPreferences pref;
    SharedPreferences.Editor editor;
    Context _context;
    int PRIVATE_MODE = 0;

    // All Shared Preferences Keys    private static final String IS_USER_LOGIN = "IsUserLoggedIn";

    private static final String PREFER_NAME = "LoginPreference";
    public static String user_id = "u_id";
    public static String user_name = "u_name";



    public LoginPreference(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    public void clear() {
        editor.clear();
        editor.commit();
    }

    public void set_login_value(String u_id ,String u_name) {
        // Storing login value as TRUE
        editor.putBoolean(IS_USER_LOGIN, true);

        editor.putString(user_id, u_id);
        editor.putString(user_name, u_name);

        editor.commit();
    }


    /**     * Get stored session data     * */    public HashMap<String, String> getUserDetails(){

        //Use hashmap to store user credentials        HashMap<String, String> user = new HashMap<String, String>();
        // user name        user.put(user_id, pref.getString(user_id, null));
        user.put(user_name, pref.getString(user_name, null));


        // return user        return user;
    }

    // Check for login    public boolean isUserLoggedIn(){
        return pref.getBoolean(IS_USER_LOGIN, false);
    }


    public boolean checkLogin() {
        // Check login status        if(!this.isUserLoggedIn()){

            // user is not logged in redirect him to Login Activity            Intent i = new Intent(_context, MainActivity.class);

            // Closing all the Activities from stack            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            // Add new Flag to start new Activity            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            // Staring Login Activity            _context.startActivity(i);

            return true;
        }
        return false;
    }


    public void logoutUser() {

// Clearing all user data from Shared Preferences        editor.clear();
        editor.commit();

// After logout redirect user to Login Activity        Intent i = new Intent(_context, MainActivity.class);

        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Staring Login Activity        _context.startActivity(i);


    }
} 

Comments

Popular posts from this blog

retrofil gjstatus lanuage

form object

Login Preference in android create class