Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- email = (EditText) findViewById(R.id.email);
- password = (EditText) findViewById(R.id.password);
- login = (Button) findViewById(R.id.login);
- signup = (TextView) findViewById(R.id.open_signup);
- progressDialog = new ProgressDialog(this);
- session = new UserSession(this);
- userInfo = new UserInfo(this);
- if (session.isUserLoggedin()) {
- startActivity(new Intent(this, MainActivity.class));
- finish();
- }
- login.setOnClickListener(this);
- signup.setOnClickListener(this);
- }
- private void login(final String email, final String password) {
- //Tag used to cancel the request
- String tag_string_req = "req_login";
- progressDialog.setMessage("Logging in....");
- progressDialog.show();
- StringRequest strReq = new StringRequest(Request.Method.POST,
- Utils.LOGIN_URL, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.d(TAG, "Login Response: " + response.toString());
- try {
- JSONObject jObj = new JSONObject(response);
- boolean error = jObj.getBoolean("error");
- //check for error node in json
- if (!error) {
- //now store the user in SQLite
- JSONObject user = jObj.getJSONObject("user");
- String uName = user.getString("username");
- String email = user.getString("email");
- //Inserting row in users table
- userInfo.setEmail(email);
- userInfo.setUsername(uName);
- session.setLoggedin(true);
- startActivity(new Intent(Login.this, MainActivity.class));
- finish();
- } else {
- //error in login, get the error message
- String errorMsg = jObj.getString("error_msg");
- toast(errorMsg);
- }
- } catch (JSONException e) {
- //json error
- e.printStackTrace();
- toast("Json error: " + e.getMessage());
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.e(TAG, "Login Error: " + error.getMessage());
- toast("Unknown Error occured");
- progressDialog.hide();
- }
- }) {
- @Override
- protected Map<String, String> getParams() {
- //Posting parameters to login url
- Map<String, String> params = new HashMap<>();
- params.put("email", email);
- params.put("password", password);
- return params;
- }
- ;
- //Adding request to request queue
- AndroidLoginController.getInstance();
- addToRequestQueue(strReq, tag_string_req);
- }
- private void toast(String x) {
- Toast.makeText(this, x, Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.login:
- String uName = email.getText().toString().trim();
- String pass = password.getText().toString().trim();
- login(uName, pass);
- break;
- case R.id.open_signup:
- startActivity(new Intent(this, SignUp.class));
- break;
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
- }
- /**
- * ATTENTION: This was auto-generated to implement the App Indexing API.
- * See https://g.co/AppIndexing/AndroidStudio for more information.
- */
- public Action getIndexApiAction() {
- Thing object = new Thing.Builder()
- .setName("Login Page") // TODO: Define a title for the content shown.
- // TODO: Make sure this auto-generated URL is correct.
- .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
- .build();
- return new Action.Builder(Action.TYPE_VIEW)
- .setObject(object)
- .setActionStatus(Action.STATUS_TYPE_COMPLETED)
- .build();
- }
- @Override
- public void onStart() {
- super.onStart();
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- client.connect();
- AppIndex.AppIndexApi.start(client, getIndexApiAction());
- }
- @Override
- public void onStop() {
- super.onStop();
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- AppIndex.AppIndexApi.end(client, getIndexApiAction());
- client.disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement