Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. public class RestService {
  2.  
  3. public static Retrofit retrofit = null;
  4.  
  5. public static Retrofit getClient(String url) {
  6. if (retrofit == null) {
  7. retrofit = new Retrofit.Builder()
  8. .baseUrl(url)
  9. .addConverterFactory(GsonConverterFactory.create())
  10. .build();
  11. }
  12. return retrofit;
  13. }
  14.  
  15. public class ApiUtils {
  16.  
  17. public static final String NService = "http://localhost:5108/NSureServices.svc/";
  18.  
  19. public static Service getLoginDetails(){
  20. return RestService.getClient(NService).create(Service.class);
  21. }
  22.  
  23. public interface Service {
  24.  
  25. @FormUrlEncoded
  26. @POST("GetLoginDetails")
  27. Call<User> login(@Field("USERID") String UserId, @Field("PASSWORD") String Password, @Field("apiKey") String ApiKey);
  28.  
  29. public class LoginActivity extends AppCompatActivity {
  30.  
  31. EditText email, password;
  32. Button login;
  33. TextView register, forgotPassword;
  34. Service service;
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_login);
  39.  
  40. email = (EditText)findViewById(R.id.emailIdEt);
  41. password = (EditText)findViewById(R.id.passwordEt);
  42. register = (TextView)findViewById(R.id.signUpTv);
  43. forgotPassword = (TextView)findViewById(R.id.forgotPasswordTv);
  44. login = (Button)findViewById(R.id.loginBtn);
  45. service = ApiUtils.getLoginDetails();
  46.  
  47. login.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View view) {
  50. User user = new User();
  51. user.setUSERID(email.getText().toString());
  52. user.setPASSWORD(password.getText().toString());
  53.  
  54.  
  55. if(validateLogin(user.getUSERID(),user.getPASSWORD())){
  56. doLogin(user.getUSERID(),user.getPASSWORD(),user.getApiKey());
  57. }
  58.  
  59. }
  60. });
  61.  
  62. }
  63.  
  64. public boolean validateLogin(String UserId, String Password){
  65. if(UserId == null || UserId.trim().length() == 0){
  66. Toast.makeText(this, "Username is required", Toast.LENGTH_LONG).show();
  67. return false;
  68. }
  69. if(Password == null || Password.trim().length() == 0){
  70. Toast.makeText(this, "Password is required", Toast.LENGTH_LONG).show();
  71. return false;
  72. }
  73. return true;
  74. }
  75.  
  76.  
  77.  
  78. public void doLogin(String uid, String pwd, String apikey){
  79. try {
  80. Call<User> call =service.login(uid, pwd, apikey);
  81. call.enqueue(new Callback<User>() {
  82. @Override
  83. public void onResponse(Call<User> call, Response<User> response) {
  84. if(response.isSuccessful()){
  85. Toast.makeText(LoginActivity.this, "Successful", Toast.LENGTH_LONG).show();
  86. }
  87. else{
  88. Toast.makeText(LoginActivity.this, "Failure", Toast.LENGTH_LONG).show();
  89.  
  90. }
  91. }
  92.  
  93. @Override
  94. public void onFailure(Call<User> call, Throwable t) {
  95. Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_LONG).show();
  96.  
  97. }
  98. });
  99.  
  100. } catch (Exception ex){
  101. Toast.makeText(LoginActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();
  102.  
  103. }
  104.  
  105. }
  106.  
  107. dependencies {
  108. compile fileTree(dir: 'libs', include: ['*.jar'])
  109. androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  110. exclude group: 'com.android.support', module: 'support-annotations'
  111. })
  112. compile 'com.android.support:appcompat-v7:26.+'
  113. compile 'com.android.support.constraint:constraint-layout:1.0.2'
  114. compile 'com.squareup.retrofit2:retrofit:2.3.0'
  115. compile 'com.google.code.gson:gson:2.6.1'
  116. compile 'com.squareup.retrofit2:converter-gson:2.3.0'
  117. compile 'com.squareup.okhttp:okhttp:2.4.0'
  118. provided 'org.glassfish:javax.annotation:10.0-b28'
  119.  
  120. compile 'com.android.support:support-v4:26.+'
  121. testCompile 'junit:junit:4.12'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement