Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mllsh.oborudovanie2;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ProgressBar;
- import android.widget.Toast;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.HashMap;
- public class activity_autoriz extends AppCompatActivity {
- EditText editLogin, editPassword;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_autoriz);
- editLogin = (EditText) findViewById(R.id.editlogin);
- editPassword = (EditText) findViewById(R.id.editPassword);
- findViewById(R.id.singin).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- userLogin();
- }
- });
- //if user presses on not registered
- findViewById(R.id.textViewRegister).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //open register screen
- finish();
- startActivity(new Intent(getApplicationContext(), MainActivity.class));
- }
- });
- }
- private void userLogin() {
- //first getting the values
- final String login = editLogin.getText().toString();
- final String password = editPassword.getText().toString();
- //validating inputs
- if (TextUtils.isEmpty(login)) {
- editLogin.setError("Please enter your username");
- editLogin.requestFocus();
- return;
- }
- if (TextUtils.isEmpty(password)) {
- editPassword.setError("Please enter your password");
- editPassword.requestFocus();
- return;
- }
- //if everything is fine
- class userLogin extends AsyncTask <Void, Void, String> {
- ProgressBar progressBar;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- progressBar = (ProgressBar) findViewById(R.id.progressBar);
- progressBar.setVisibility(View.VISIBLE);
- }
- @Override
- protected void onPostExecute(String s) {
- super.onPostExecute(s);
- progressBar.setVisibility(View.GONE);
- try {
- //converting response to json object
- JSONObject obj = new JSONObject(s);
- //if no error in response
- if (!obj.getBoolean("error")) {
- Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
- //getting the user from the response
- JSONObject userJson = obj.getJSONObject("user");
- //creating a new user object
- User user = new User(
- userJson.getInt("id"),
- userJson.getString("login"),
- userJson.getString("username"),
- userJson.getString("fio"),
- userJson.getString("otchestvo"),
- userJson.getString("rol")
- );
- //storing the user in shared preferences
- SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);
- //starting the profile activity
- finish();
- if(user.getRol().equals("admin") || user.getRol().equals("snab")){
- startActivity(new Intent(getApplicationContext(), AdminActivity.class));
- }else{
- startActivity(new Intent(getApplicationContext(), Snab.class));
- }} else {
- Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected String doInBackground(Void... voids) {
- //creating request handler object
- RequestHandler requestHandler = new RequestHandler();
- //creating request parameters
- HashMap<String, String> params = new HashMap<>();
- params.put("login", login);
- params.put("password", password);
- //returing the response
- return requestHandler.sendPostRequest(URLs.URL_LOGIN, params);
- }
- }
- userLogin ul = new userLogin();
- ul.execute();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment