Advertisement
Guest User

Untitled

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