Guest User

Untitled

a guest
Sep 7th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. package com.vshine.neuron.riseshine.activity;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.ImageButton;
  11. import android.widget.Toast;
  12.  
  13. import com.vshine.neuron.riseshine.R;
  14. import com.vshine.neuron.riseshine.util.SessionManager;
  15. import com.vshine.neuron.riseshine.webservice.ApiClient;
  16. import com.vshine.neuron.riseshine.webservice.ApiInterface;
  17. import com.vshine.neuron.riseshine.webservice.apimodel.login.LoginResponse;
  18.  
  19. import retrofit2.Call;
  20. import retrofit2.Callback;
  21. import retrofit2.Response;
  22.  
  23. public class LoginActivity extends AppCompatActivity {
  24. Button login_button;
  25. ImageButton GetRegistered;
  26. EditText userName_edt, password_edt;
  27. String user_name, password;
  28. SessionManager sessionManager;
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_login);
  34.  
  35. sessionManager = new SessionManager(LoginActivity.this);
  36. if (sessionManager.isLoggedIn()) {
  37. startActivity(new Intent(getApplicationContext(), MainActivity.class));
  38. this.finish();
  39.  
  40. } else {
  41. initBasic();
  42. initLogin();
  43. }
  44.  
  45.  
  46. }
  47.  
  48. private void initBasic() {
  49. login_button = findViewById(R.id.login_button);
  50. GetRegistered = findViewById(R.id.register);
  51. userName_edt = findViewById(R.id.user_name);
  52. password_edt = findViewById(R.id.password);
  53.  
  54. }
  55.  
  56. private void initLogin() {
  57. GetRegistered.setOnClickListener(new View.OnClickListener() {
  58.  
  59. @Override
  60. public void onClick(View v) {
  61. Intent intent = new Intent(LoginActivity.this, RegisteredActivity.class);
  62. startActivity(intent);
  63. }
  64. });
  65. login_button.setOnClickListener(new View.OnClickListener() {
  66.  
  67. @Override
  68. public void onClick(View v) {
  69. user_name = userName_edt.getText().toString();
  70. password = password_edt.getText().toString();
  71. if (validateLogin(user_name, password)) {
  72. //do login
  73. doLogin(user_name, password);
  74. }
  75. }
  76. });
  77. }
  78.  
  79. private boolean validateLogin(String user_name, String password) {
  80. if (user_name == null || user_name.trim().length() == 0) {
  81. Toast.makeText(this, "Username is required", Toast.LENGTH_SHORT).show();
  82. return false;
  83. }
  84. if (password == null || password.trim().length() == 0) {
  85. Toast.makeText(this, "Password is required", Toast.LENGTH_SHORT).show();
  86. return false;
  87. }
  88. return true;
  89. }
  90.  
  91. private void doLogin(final String username, final String password) {
  92.  
  93. ApiInterface apiService =
  94. ApiClient.getClient().create(ApiInterface.class);
  95.  
  96. Call<LoginResponse> call = apiService.Login(username, password);
  97. call.enqueue(new Callback<LoginResponse>() {
  98. @Override
  99. public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
  100.  
  101. LoginResponse loginresponse = response.body();
  102. if (!loginresponse.getStatus()) {
  103. /* AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this).create();
  104. alertDialog.setTitle("Alert");
  105. alertDialog.setMessage("message");
  106. alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
  107. new DialogInterface.OnClickListener() {
  108. public void onClick(DialogInterface dialog, int which) {
  109. dialog.dismiss();
  110. }
  111. });
  112. alertDialog.show();*/
  113. Toast.makeText(LoginActivity.this, response.code() + "response", Toast.LENGTH_SHORT).show();
  114. } else {
  115. String id = loginresponse.getResponse().getId();
  116. String userName = loginresponse.getResponse().getUsername();
  117. String firstName = loginresponse.getResponse().getFirstName();
  118. String lastName = loginresponse.getResponse().getLastName();
  119. String email = loginresponse.getResponse().getEmail();
  120. sessionManager.createLoginSession(id, userName, email, firstName, lastName);
  121.  
  122. startActivity(new Intent(getApplicationContext(), MainActivity.class));
  123. finish();
  124. }
  125. }
  126.  
  127. @Override
  128. public void onFailure(Call<LoginResponse> call, Throwable t) {
  129. // Log error here since request failed
  130. Log.e("", t.toString());
  131. }
  132. });
  133.  
  134. }
  135. }
  136.  
  137. package com.vshine.neuron.riseshine.webservice;
  138.  
  139. import java.util.concurrent.TimeUnit;
  140.  
  141. import okhttp3.OkHttpClient;
  142. import retrofit2.Retrofit;
  143. import retrofit2.converter.gson.GsonConverterFactory;
  144.  
  145. public class ApiClient {
  146.  
  147. public static final String BASE_URL = "http://........../vshine/API/";
  148. private static Retrofit retrofit = null;
  149.  
  150.  
  151. public static Retrofit getClient() {
  152. OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
  153. .connectTimeout(10, TimeUnit.SECONDS)
  154. .readTimeout(10, TimeUnit.SECONDS)
  155. .writeTimeout(10, TimeUnit.SECONDS)
  156. .build();
  157. if (retrofit == null) {
  158. retrofit = new Retrofit.Builder()
  159. .baseUrl(BASE_URL)
  160. .client(okHttpClient)
  161. .addConverterFactory(GsonConverterFactory.create())
  162. .build();
  163. }
  164. return retrofit;
  165.  
  166. }
  167. }
  168.  
  169. package com.vshine.neuron.riseshine.webservice;
  170.  
  171.  
  172. import com.google.gson.JsonObject;
  173. import com.vshine.neuron.riseshine.webservice.apimodel.login.LoginResponse;
  174. import retrofit2.Call;
  175. import retrofit2.http.Field;
  176. import retrofit2.http.FormUrlEncoded;
  177. import retrofit2.http.POST;
  178.  
  179. public interface ApiInterface {
  180. @FormUrlEncoded
  181. @POST("api.php?action=Login")
  182. Call<LoginResponse> Login(@Field("user_name") String username, @Field("password") String password);
  183.  
  184. }
Add Comment
Please, Sign In to add comment