Guest User

Untitled

a guest
Mar 30th, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. package com.india.gov.helperapp;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.widget.Toast;
  7. import android.util.Log;
  8.  
  9. import android.content.Intent;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14.  
  15. import java.io.IOException;
  16.  
  17. import butterknife.ButterKnife;
  18. import butterknife.Bind;
  19. import okhttp3.MediaType;
  20. import okhttp3.OkHttpClient;
  21. import okhttp3.Request;
  22. import okhttp3.RequestBody;
  23. import okhttp3.Response;
  24.  
  25. /**
  26. * Created by vyas on 3/25/17.
  27. */
  28.  
  29. public class LoginActivity extends AppCompatActivity {
  30. private static final String TAG = "LoginActivity";
  31. private static final int REQUEST_SIGNUP = 0;
  32.  
  33. @Bind(R.id.input_username) EditText _usernameText;
  34. @Bind(R.id.input_password) EditText _passwordText;
  35. @Bind(R.id.btn_login) Button _loginButton;
  36. @Bind(R.id.link_signup) TextView _signupLink;
  37.  
  38. @Override
  39. public void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_login);
  42. ButterKnife.bind(this);
  43.  
  44. _loginButton.setOnClickListener(new View.OnClickListener() {
  45.  
  46. @Override
  47. public void onClick(View v) {
  48. login();
  49. }
  50. });
  51.  
  52. _signupLink.setOnClickListener(new View.OnClickListener() {
  53.  
  54. @Override
  55. public void onClick(View v) {
  56. // Start the Signup activity
  57. Intent intent = new Intent(getApplicationContext(), SignupActivity.class);
  58. startActivityForResult(intent, REQUEST_SIGNUP);
  59. finish();
  60. }
  61. });
  62. }
  63.  
  64. public void login() {
  65. Log.d(TAG, "Login");
  66.  
  67. if (!validate()) {
  68. onLoginFailed();
  69. return;
  70. }
  71.  
  72. _loginButton.setEnabled(false);
  73.  
  74. final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
  75. progressDialog.setIndeterminate(true);
  76. progressDialog.setMessage("Authenticating...");
  77. progressDialog.show();
  78.  
  79.  
  80. // TODO: Implement your own authentication logic here.
  81.  
  82. new android.os.Handler().postDelayed(
  83. new Runnable() {
  84. public void run() {
  85. String username = _usernameText.getText().toString();
  86. String password = _passwordText.getText().toString();
  87.  
  88. String result;
  89. MediaType JSON
  90. = MediaType.parse("application/json; charset=utf-8");
  91.  
  92. OkHttpClient client = new OkHttpClient();
  93. String json = "{username:\""+username+"\",password:\""+password+"\"}";
  94. String url = "192.168.43.143:8080";
  95. RequestBody body = RequestBody.create(JSON, json);
  96. Request request = new Request.Builder()
  97. .url(url)
  98. .post(body)
  99. .build();
  100. Response response = null;
  101. try {
  102. response = client.newCall(request).execute();
  103. result = response.body().string();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. return;
  107. }
  108.  
  109. onLoginSuccess();
  110. // onLoginFailed();
  111. progressDialog.dismiss();
  112. }
  113. }, 3000);
  114. }
  115.  
  116.  
  117. @Override
  118. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  119. if (requestCode == REQUEST_SIGNUP) {
  120. if (resultCode == RESULT_OK) {
  121.  
  122. // TODO: Implement successful signup logic here
  123. // By default we just finish the Activity and log them in automatically
  124. this.finish();
  125. }
  126. }
  127. }
  128.  
  129. @Override
  130. public void onBackPressed() {
  131. // Disable going back to the MainActivity
  132. moveTaskToBack(true);
  133. }
  134.  
  135. public void onLoginSuccess() {
  136. _loginButton.setEnabled(true);
  137. finish();
  138. }
  139.  
  140. public void onLoginFailed() {
  141. Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
  142.  
  143. _loginButton.setEnabled(true);
  144. }
  145.  
  146. public boolean validate() {
  147. boolean valid = true;
  148.  
  149. String password = _passwordText.getText().toString();
  150.  
  151. if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
  152. _passwordText.setError("between 4 and 10 alphanumeric characters");
  153. valid = false;
  154. } else {
  155. _passwordText.setError(null);
  156. }
  157.  
  158. return valid;
  159. }
  160. }
Add Comment
Please, Sign In to add comment