Guest User

Untitled

a guest
Mar 30th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. package com.india.gov.helperapp;
  2.  
  3.  
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Spinner;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import java.io.IOException;
  17. import java.text.ParseException;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20.  
  21. import butterknife.ButterKnife;
  22. import butterknife.Bind;
  23. import okhttp3.MediaType;
  24. import okhttp3.OkHttpClient;
  25. import okhttp3.Request;
  26. import okhttp3.RequestBody;
  27. import okhttp3.Response;
  28.  
  29. /**
  30. * Created by vyas on 3/25/17.
  31. */
  32.  
  33. public class SignupActivity extends AppCompatActivity {
  34. private static final String TAG = "SignupActivity";
  35.  
  36. @Bind(R.id.input_username) EditText _usernameText;
  37. @Bind(R.id.input_name) EditText _nameText;
  38. @Bind(R.id.input_address) EditText _addressText;
  39. @Bind(R.id.input_email) EditText _emailText;
  40. @Bind(R.id.input_mobile) EditText _mobileText;
  41. @Bind(R.id.input_password) EditText _passwordText;
  42. @Bind(R.id.input_reEnterPassword) EditText _reEnterPasswordText;
  43. @Bind(R.id.btn_signup) Button _signupButton;
  44. @Bind(R.id.link_login) TextView _loginLink;
  45. @Bind(R.id.input_adhaar) TextView _adhaarText;
  46. @Bind(R.id.input_city) TextView _cityText;
  47. @Bind(R.id.input_dob) TextView _dobText;
  48. @Bind(R.id.input_pan) TextView _panText;
  49. @Bind(R.id.input_gname) TextView _gnameText;
  50. @Bind(R.id.etype_spinner) Spinner _etypeSpinner;
  51.  
  52. String username;
  53. String name;
  54. String address;
  55. String email;
  56. String mobile;
  57. String password;
  58. String reEnterPassword;
  59. Integer adhaar;
  60. String city;
  61. int pan;
  62. String gname;
  63. Date dob;
  64.  
  65. @Override
  66. public void onCreate(Bundle savedInstanceState) {
  67. super.onCreate(savedInstanceState);
  68. setContentView(R.layout.activity_signup);
  69. ButterKnife.bind(this);
  70.  
  71. _signupButton.setOnClickListener(new View.OnClickListener() {
  72. @Override
  73. public void onClick(View v) {
  74. signup();
  75. }
  76. });
  77.  
  78. _loginLink.setOnClickListener(new View.OnClickListener() {
  79. @Override
  80. public void onClick(View v) {
  81. // Finish the registration screen and return to the Login activity
  82. Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
  83. startActivity(intent);
  84. finish();
  85. }
  86. });
  87. }
  88.  
  89. public void signup() {
  90. Log.d(TAG, "Signup");
  91.  
  92. /*
  93. if (!validate()) {
  94. onSignupFailed();
  95. return;
  96. }
  97. */
  98.  
  99. _signupButton.setEnabled(false);
  100.  
  101. final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this);
  102. progressDialog.setIndeterminate(true);
  103. progressDialog.setMessage("Creating Account...");
  104. progressDialog.show();
  105.  
  106. username = _usernameText.getText().toString();
  107. name = _nameText.getText().toString();
  108. address = _addressText.getText().toString();
  109. email = _emailText.getText().toString();
  110. mobile = _mobileText.getText().toString();
  111. password = _passwordText.getText().toString();
  112. reEnterPassword = _reEnterPasswordText.getText().toString();
  113. adhaar = Integer.parseInt(_adhaarText.getText().toString());
  114. city = _cityText.getText().toString();
  115. pan = Integer.parseInt(_panText.getText().toString());
  116. gname = _gnameText.getText().toString();
  117.  
  118. SimpleDateFormat parser=new SimpleDateFormat("d m yyyy");
  119. try {
  120. dob = parser.parse(_dobText.getText().toString());
  121. } catch (ParseException e) {
  122. Toast.makeText(this,"Invalid date format. Use d-m-yyyy.",Toast.LENGTH_LONG).show();
  123. onSignupFailed();
  124. return;
  125. }
  126. // TODO: Implement your own signup logic here.
  127. new android.os.Handler().postDelayed(
  128. new Runnable() {
  129. public void run() {
  130. String result;
  131. MediaType JSON
  132. = MediaType.parse("application/json; charset=utf-8");
  133.  
  134. OkHttpClient client = new OkHttpClient();
  135. String json = "{username:\""+username+"\",password:\""+password+"\"}";
  136. String url = "192.168.43.143:8080";
  137. RequestBody body = RequestBody.create(JSON, json);
  138. Request request = new Request.Builder()
  139. .url(url)
  140. .post(body)
  141. .build();
  142. Response response = null;
  143. try {
  144. response = client.newCall(request).execute();
  145. result = response.body().string();
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. return;
  149. }
  150. onSignupSuccess();
  151. // onSignupFailed();
  152. progressDialog.dismiss();
  153. }
  154. }, 3000);
  155. }
  156.  
  157.  
  158. public void onSignupSuccess() {
  159. _signupButton.setEnabled(true);
  160. setResult(RESULT_OK, null);
  161. finish();
  162. }
  163.  
  164. public void onSignupFailed() {
  165. Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
  166.  
  167. _signupButton.setEnabled(true);
  168. }
  169.  
  170. public boolean validate() {
  171. boolean valid = true;
  172.  
  173. if (name.isEmpty() || name.length() < 3) {
  174. _nameText.setError("at least 3 characters");
  175. valid = false;
  176. } else {
  177. _nameText.setError(null);
  178. }
  179.  
  180. if (address.isEmpty()) {
  181. _addressText.setError("Enter Valid Address");
  182. valid = false;
  183. } else {
  184. _addressText.setError(null);
  185. }
  186.  
  187.  
  188. if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
  189. _emailText.setError("enter a valid email address");
  190. valid = false;
  191. } else {
  192. _emailText.setError(null);
  193. }
  194.  
  195. if (mobile.isEmpty() || mobile.length()!=10) {
  196. _mobileText.setError("Enter Valid Mobile Number");
  197. valid = false;
  198. } else {
  199. _mobileText.setError(null);
  200. }
  201.  
  202. if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
  203. _passwordText.setError("between 4 and 10 alphanumeric characters");
  204. valid = false;
  205. } else {
  206. _passwordText.setError(null);
  207. }
  208.  
  209. if (reEnterPassword.isEmpty() || reEnterPassword.length() < 4 || reEnterPassword.length() > 10 || !(reEnterPassword.equals(password))) {
  210. _reEnterPasswordText.setError("Password Do not match");
  211. valid = false;
  212. } else {
  213. _reEnterPasswordText.setError(null);
  214. }
  215.  
  216. return valid;
  217. }
  218. }
Add Comment
Please, Sign In to add comment