Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. package hr.powersoft.metus.Login;
  2.  
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.preference.PreferenceManager;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.CheckBox;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14.  
  15. import org.json.JSONArray;
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18. import org.w3c.dom.Document;
  19. import org.w3c.dom.Element;
  20.  
  21. import hr.powersoft.metus.MainActivity;
  22. import hr.powersoft.metus.R;
  23. import hr.powersoft.metus.XmlParser;
  24. import hr.powersoft.web.ActivityConstants;
  25. import hr.powersoft.web.AsyncRespListener;
  26. import hr.powersoft.web.WebParams;
  27. import hr.powersoft.web.WebRequest;
  28.  
  29.  
  30. public class LoginActivity extends AppCompatActivity {
  31.  
  32. private Button button;
  33. private EditText etAppKey;
  34. private EditText etUsername;
  35. private EditText etPassword;
  36. private String appKey = "";
  37. private String username = "";
  38. private String password = "";
  39. private String catalog = "";
  40. private String cipher = "";
  41. private Intent intent;
  42. private CheckBox checkBoxRemeber;
  43.  
  44. @Override
  45. protected void onCreate(Bundle savedInstanceState) {
  46. super.onCreate(savedInstanceState);
  47. setContentView(R.layout.activity_login);
  48.  
  49. button = (Button)findViewById(R.id.buttonLogin);
  50. etAppKey = (EditText)findViewById(R.id.app_key);
  51. etAppKey = (EditText) findViewById(R.id.app_key);
  52. etUsername = (EditText)findViewById(R.id.etUsername);
  53. etPassword = (EditText)findViewById(R.id.etPassword);
  54. checkBoxRemeber = (CheckBox)findViewById(R.id.checkBoxRemember);
  55.  
  56. intent = getIntent();
  57.  
  58. if (intent != null)
  59. {
  60. if(intent.getStringExtra("username") != null && intent.getStringExtra("password") != null)
  61. {
  62. etUsername.setText(intent.getStringExtra("username"));
  63. etPassword.setText(intent.getStringExtra("password"));
  64. }
  65. }
  66.  
  67. button.setOnClickListener(new View.OnClickListener() {
  68. @Override
  69. public void onClick(View v) {
  70.  
  71. findViewById(R.id.avloadingIndicatorView).setVisibility(View.VISIBLE);
  72.  
  73. appKey = etAppKey.getText().toString();
  74. username = etUsername.getText().toString();
  75. password = etPassword.getText().toString();
  76.  
  77. if(appKey.isEmpty())
  78. appKey = "";
  79. if(username.isEmpty())
  80. username = "";
  81. if(password.isEmpty())
  82. password = "";
  83.  
  84. WebParams params = new WebParams();
  85. params.postParameters = "appkey=" + appKey + "&uname=" + username + "&pwd=" + password;
  86. params.listener = listener;
  87. new WebRequest(ActivityConstants.ActivityConst.LOGIN_ACTIVITY).execute(params);
  88. }
  89. });
  90.  
  91. }
  92.  
  93. AsyncRespListener listener = new AsyncRespListener() {
  94. @Override
  95. public void processFinished(String result) {
  96. findViewById(R.id.avloadingIndicatorView).setVisibility(View.GONE);
  97.  
  98. if (result == null || result.isEmpty())
  99. {
  100. Toast.makeText(LoginActivity.this, "Greška pri povezivanju.", Toast.LENGTH_SHORT).show();
  101. return;
  102. }
  103.  
  104. XmlParser xmlParser = new XmlParser();
  105. Document document = xmlParser.getDomElement(result);
  106. Element root = document.getDocumentElement();
  107. String resultString = root.getTextContent();
  108.  
  109. if (resultString == "Authentication failed!") {
  110. Toast.makeText(LoginActivity.this, "Greška pri autentikaciji.", Toast.LENGTH_SHORT).show();
  111. return;
  112. }
  113.  
  114. JSONArray jsonArray = null;
  115. JSONObject jsonObject = null;
  116.  
  117. try
  118. {
  119. jsonArray = new JSONArray(resultString);
  120. if(jsonArray.length() == 0) throw new JSONException("Nema podataka");
  121.  
  122. jsonObject = jsonArray.getJSONObject(0);
  123. username = jsonObject.getString("korisnickoime");
  124. password = jsonObject.getString("lozinka");
  125. catalog = jsonObject.getString("Katalog");
  126. cipher = jsonObject.getString("Sifra");
  127.  
  128. if(checkBoxRemeber.isChecked())
  129. {
  130. PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("remember", username);
  131. }
  132.  
  133. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  134. startActivity(intent);
  135. finish();
  136. }
  137. catch (JSONException e)
  138. {
  139. Toast.makeText(getApplicationContext(),"Greška pri autentikaciji.",Toast.LENGTH_SHORT).show();
  140. }
  141. }
  142. };
  143.  
  144. @Override
  145. protected void onPause() {
  146. super.onPause();
  147. saveCollection();
  148. }
  149.  
  150. private void saveCollection() {
  151. PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("appKey",appKey);
  152. PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("catalog",catalog);
  153. PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("cipher", cipher);
  154. PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("username", username);
  155. PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("password", password);
  156. PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().apply();
  157. }
  158.  
  159. @Override
  160. protected void onResume() {
  161. super.onResume();
  162. Log.e("TEST", PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("appKey",""));
  163. etAppKey = (EditText) findViewById(R.id.app_key);
  164. //etAppKey.setText(preferences.getString("appKey",""));
  165. }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement