Guest User

Untitled

a guest
May 5th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. get("/getUser/:user",(request,response)-> {
  2. response.type("application/json");
  3. Account acc = gson.fromJson(request.params(":user"), Account.class);
  4. return getUser(acc);
  5. }, gson ::toJson);
  6.  
  7. private static Info getUser(Account acc) throws ClassNotFoundException, SQLException, UnsupportedEncodingException, NoSuchAlgorithmException {
  8. Connection c;
  9. Statement stm = null;
  10. Class.forName("org.postgresql.Driver");
  11. c = DriverManager.getConnection("jdbc:postgresql://<myIP>:5432/egecalc", "postgres", "itsMyAppBitches");
  12. String sql = "select * from users;";
  13. Info result = null;
  14. String hash = getHash(acc.getPassword());
  15. stm = c.createStatement();
  16. ResultSet rs = stm.executeQuery(sql);
  17. while (rs.next()) {
  18. if (rs.getString("username").equals(acc.getUsername())) {
  19. if (rs.getString("password").equals(acc.getPassword())) {
  20. result = new Info("successful");
  21. break;
  22. } else {
  23. result = new Info("invalid password");
  24. break;
  25. }
  26. }
  27. else
  28. result = new Info("invalid username");
  29. }
  30. rs.close();
  31. c.close();
  32. return result;
  33. }
  34.  
  35. public interface getUser{
  36. @GET("/getUser/{user}")
  37. Call<Info> CurrUser(@Path("user") String str);
  38. }
  39.  
  40. public class Info {
  41. @SerializedName("information")
  42. @Expose
  43. private String information;
  44.  
  45. public Info(String information) {
  46. this.information = information;
  47. }
  48.  
  49. public String getInformation() {
  50. return information;
  51. }
  52.  
  53. public class AutirizationAPI {
  54. private static String BASE_URL = "http://<myIP>:4567";
  55. public static EgeCalcApi.getUser getApi() {
  56. Gson gson = new GsonBuilder()
  57. .setLenient()
  58. .create();
  59. Retrofit retrofit = new Retrofit.Builder()
  60. .baseUrl(BASE_URL)
  61. .addConverterFactory(GsonConverterFactory.create(gson))
  62. .build();
  63.  
  64. EgeCalcApi.getUser getuser = retrofit.create(EgeCalcApi.getUser.class);
  65. return getuser;
  66.  
  67. }
  68.  
  69. public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
  70.  
  71. private final String mLogin;
  72. private final String mPassword;
  73. private String mError = "1";
  74. private int i = 1;
  75. private EgeCalcApi.getUser getUser;
  76. private Account acc;
  77. UserLoginTask(String login, String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {
  78. mLogin = login;
  79. mPassword = getHash(password);
  80. acc = new Account(mLogin, mPassword, "",0);
  81. }
  82.  
  83. @Override
  84. protected Boolean doInBackground(Void... params) {
  85. // TODO: attempt authentication against a network service.
  86. Gson gson = new Gson();
  87. String str = gson.toJson(acc);
  88. getUser = AutirizationAPI.getApi();
  89. getUser.CurrUser(str).enqueue(new Callback<Info>() {
  90. @Override
  91. public void onResponse(@NonNull Call<Info> call, @NonNull Response<Info> response) {
  92. System.out.print(response.body());
  93. Info info = response.body();
  94. assert info != null;
  95. mError = info.getInformation();
  96. System.out.print(mError);
  97. if (mError.equals("successful"))
  98. i = 0;
  99. else {
  100. i = 1;
  101. }
  102. }
  103.  
  104. @Override
  105. public void onFailure(@NonNull Call<Info> call, @NonNull Throwable t) {
  106. Toast.makeText(LoginActivity.this, "Ошибка сети" + t, Toast.LENGTH_SHORT).show();
  107. }
  108. });
  109. return i == 0;
  110. }
  111.  
  112. @Override
  113. protected void onPostExecute(final Boolean success) {
  114. mAuthTask = null;
  115. showProgress(false);
  116.  
  117. if (success) {
  118. Intent i = new Intent(LoginActivity.this, MainActivity.class);
  119. startActivity(i);
  120. finish();
  121. } else {
  122. if(mError.equals("invalid password")) {
  123. mPasswordView.setError(mError);
  124. mPasswordView.requestFocus();
  125. }
  126. else if(mError.equals("invalid username")){
  127. mEmailView.setError(mError);
  128. mEmailView.requestFocus();
  129. }
  130. }
  131. }
  132.  
  133. @Override
  134. protected void onCancelled() {
  135. mAuthTask = null;
  136. showProgress(false);
  137. }
  138. }
  139.  
  140. @Override
  141. protected Boolean doInBackground(Void... params) {
  142. // ...
  143. getUser.CurrUser(str).enqueue(new Callback<Info>() { // Строчка (*)
  144. // Весь код ниже будет выполнен асинхронно
  145. @Override
  146. public void onResponse(@NonNull Call<Info> call, @NonNull Response<Info> response) {
  147. System.out.print(response.body());
  148. Info info = response.body();
  149. assert info != null;
  150. mError = info.getInformation();
  151. System.out.print(mError);
  152. if (mError.equals("successful"))
  153. i = 0;
  154. else {
  155. i = 1;
  156. }
  157. }
  158.  
  159. @Override
  160. public void onFailure(@NonNull Call<Info> call, @NonNull Throwable t) {
  161. Toast.makeText(LoginActivity.this, "Ошибка сети" + t, Toast.LENGTH_SHORT).show();
  162. }
  163.  
  164. // Весь код выше будет выполнен асинхронно
  165. });
  166.  
  167. // Эта строчка выполнится сразу же
  168. // после строчки (*)
  169. return i == 0;
  170. }
Add Comment
Please, Sign In to add comment