Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hr.powersoft.metus.Login;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import hr.powersoft.metus.MainActivity;
- import hr.powersoft.metus.R;
- import hr.powersoft.metus.XmlParser;
- import hr.powersoft.web.ActivityConstants;
- import hr.powersoft.web.AsyncRespListener;
- import hr.powersoft.web.WebParams;
- import hr.powersoft.web.WebRequest;
- public class LoginActivity extends AppCompatActivity {
- private Button button;
- private EditText etAppKey;
- private EditText etUsername;
- private EditText etPassword;
- private String appKey = "";
- private String username = "";
- private String password = "";
- private String catalog = "";
- private String cipher = "";
- private SharedPreferences preferences;
- private SharedPreferences.Editor editor;
- private Intent intent;
- private CheckBox checkBoxRemeber;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
- editor = preferences.edit();
- button = (Button)findViewById(R.id.buttonLogin);
- etAppKey = (EditText)findViewById(R.id.app_key);
- etAppKey = (EditText) findViewById(R.id.app_key);
- etAppKey.setText(preferences.getString("appKey",""));
- etUsername = (EditText)findViewById(R.id.etUsername);
- etPassword = (EditText)findViewById(R.id.etPassword);
- checkBoxRemeber = (CheckBox)findViewById(R.id.checkBoxRemember);
- intent = getIntent();
- if (intent != null)
- {
- if(intent.getStringExtra("username") != null && intent.getStringExtra("password") != null)
- {
- etUsername.setText(intent.getStringExtra("username"));
- etPassword.setText(intent.getStringExtra("password"));
- }
- }
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- findViewById(R.id.avloadingIndicatorView).setVisibility(View.VISIBLE);
- appKey = etAppKey.getText().toString();
- username = etUsername.getText().toString();
- password = etPassword.getText().toString();
- if(appKey.isEmpty())
- appKey = "";
- if(username.isEmpty())
- username = "";
- if(password.isEmpty())
- password = "";
- WebParams params = new WebParams();
- params.postParameters = "appkey=" + appKey + "&uname=" + username + "&pwd=" + password;
- params.listener = listener;
- new WebRequest(ActivityConstants.ActivityConst.LOGIN_ACTIVITY).execute(params);
- }
- });
- }
- AsyncRespListener listener = new AsyncRespListener() {
- @Override
- public void processFinished(String result) {
- findViewById(R.id.avloadingIndicatorView).setVisibility(View.GONE);
- if (result == null || result.isEmpty())
- {
- Toast.makeText(LoginActivity.this, "Greška pri povezivanju.", Toast.LENGTH_SHORT).show();
- return;
- }
- XmlParser xmlParser = new XmlParser();
- Document document = xmlParser.getDomElement(result);
- Element root = document.getDocumentElement();
- String resultString = root.getTextContent();
- if (resultString == "Authentication failed!") {
- Toast.makeText(LoginActivity.this, "Greška pri autentikaciji.", Toast.LENGTH_SHORT).show();
- return;
- }
- JSONArray jsonArray = null;
- JSONObject jsonObject = null;
- try
- {
- jsonArray = new JSONArray(resultString);
- if(jsonArray.length() == 0) throw new JSONException("Nema podataka");
- jsonObject = jsonArray.getJSONObject(0);
- username = jsonObject.getString("korisnickoime");
- password = jsonObject.getString("lozinka");
- catalog = jsonObject.getString("Katalog");
- cipher = jsonObject.getString("Sifra");
- if(checkBoxRemeber.isChecked())
- {
- editor.putString("remember", username);
- }
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- catch (JSONException e)
- {
- Toast.makeText(getApplicationContext(),"Greška pri autentikaciji.",Toast.LENGTH_SHORT).show();
- }
- }
- };
- @Override
- protected void onPause() {
- super.onPause();
- saveCollection();
- }
- private void saveCollection() {
- editor.clear();
- editor.putString("appKey",appKey);
- editor.putString("catalog",catalog);
- editor.putString("cipher", cipher);
- editor.putString("username", username);
- editor.putString("password", password);
- editor.commit();
- }
- @Override
- protected void onResume() {
- super.onResume();
- etAppKey = (EditText) findViewById(R.id.app_key);
- etAppKey.setText(preferences.getString("appKey",""));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement