Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. public class efetuarcadastro extends android.support.v4.app.Fragment {
  2. View myView;
  3. String url = "";
  4. String parametros = "";
  5.  
  6. @Nullable
  7. @Override
  8. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  9. myView = inflater.inflate( R.layout.efetuarcadastro, container,false);
  10.  
  11.  
  12. final EditText nome = myView.findViewById( R.id.nome );
  13. final EditText login = myView.findViewById( R.id.login );
  14. final EditText email = myView.findViewById( R.id.cadmail );
  15. final EditText senha = myView.findViewById( R.id.Asenha );
  16. final EditText confSenha = myView.findViewById( R.id.confSenha );
  17.  
  18. myView.findViewById( R.id.cad ).setOnClickListener( new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21.  
  22. ConnectivityManager connMgr = (ConnectivityManager)
  23. getActivity().getSystemService( Context.CONNECTIVITY_SERVICE );
  24. NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
  25.  
  26. if (networkInfo != null && networkInfo.isConnected()){
  27.  
  28. String name = nome.getText().toString();
  29. String user = login.getText().toString();
  30. String emails = email.getText().toString();
  31. String password = senha.getText().toString();
  32. String confpass = confSenha.getText().toString();
  33.  
  34. if (name.isEmpty() ||
  35. user.isEmpty() ||
  36. login.getText().toString().length() < 5 ||
  37. emails.isEmpty() ||
  38. !emailValidar( emails ) ||
  39. password.isEmpty() ||
  40. senha.getText().toString().length() < 8 ||
  41. confpass.isEmpty() ||
  42. !confpass.equals(password))
  43.  
  44. {
  45.  
  46. validaCampos();
  47.  
  48. } else
  49.  
  50.  
  51. {
  52.  
  53. url = "http://192.168.25.230:8888/app/cadastro.php";
  54.  
  55. parametros = "nome=" + name + "&login=" + user + "&email=" + emails + "&senha=" + password;
  56.  
  57. new SolicitaDados().execute(url);
  58.  
  59. }
  60.  
  61. } else {
  62.  
  63. Toast.makeText( getActivity().getApplicationContext(), "Nehuma Conexão foi Identificada", Toast.LENGTH_LONG).show();
  64. }
  65.  
  66. }
  67.  
  68.  
  69. private void validaCampos() {
  70.  
  71. boolean res = false;
  72. boolean sen = false;
  73. boolean qtdlogin = false;
  74. boolean qtdsenha = false;
  75. String name = nome.getText().toString();
  76. String user = login.getText().toString();
  77. String emails = email.getText().toString();
  78. String password = senha.getText().toString();
  79. String confpass = confSenha.getText().toString();
  80.  
  81. if (res = verificaCampovazio( name )) {
  82. nome.requestFocus();
  83. } else if (res = verificaCampovazio( user )) {
  84. login.requestFocus();
  85. } else if (qtdlogin = login.getText().toString().length() < 5) {
  86. login.getText().clear();
  87. login.requestFocus();
  88. } else if (res = !emailValidar( emails )) {
  89. email.requestFocus();
  90. } else if (res = verificaCampovazio( password )) {
  91. senha.requestFocus();
  92. } else if (qtdsenha = senha.getText().toString().length() < 8) {
  93. senha.getText().clear();
  94. confSenha.getText().clear();
  95. senha.requestFocus();
  96. } else if (res = verificaCampovazio( confpass )) {
  97. confSenha.requestFocus();
  98. } else if (sen = !password.equals( confpass )) {
  99. senha.getText().clear();
  100. confSenha.getText().clear();
  101. senha.requestFocus();
  102. }
  103. if (res) {
  104. AlertDialog.Builder Mcampovazio = new AlertDialog.Builder( efetuarcadastro.this.getActivity() );
  105. Mcampovazio.setTitle( "Aviso: " );
  106. Mcampovazio.setMessage( "Há campos inválidos ou sem preenchimento" );
  107. Mcampovazio.setNeutralButton( "OK", null );
  108. Mcampovazio.show();
  109. }
  110.  
  111. if (sen) {
  112. AlertDialog.Builder Mcampovazio = new AlertDialog.Builder( efetuarcadastro.this.getActivity() );
  113. Mcampovazio.setTitle( "Aviso: " );
  114. Mcampovazio.setMessage( "Senhas estão diferentes" );
  115. Mcampovazio.setNeutralButton( "OK", null );
  116. Mcampovazio.show();
  117.  
  118. }
  119.  
  120. if (qtdlogin) {
  121. AlertDialog.Builder Mcampovazio = new AlertDialog.Builder( efetuarcadastro.this.getActivity() );
  122. Mcampovazio.setTitle( "Aviso: " );
  123. Mcampovazio.setMessage( "O login não pode conter menos que 5 caracteres" );
  124. Mcampovazio.setNeutralButton( "OK", null );
  125. Mcampovazio.show();
  126.  
  127. }
  128.  
  129. if (qtdsenha) {
  130. AlertDialog.Builder Mcampovazio = new AlertDialog.Builder( efetuarcadastro.this.getActivity() );
  131. Mcampovazio.setTitle( "Aviso: " );
  132. Mcampovazio.setMessage( "A senha não pode conter menos que 8 caracteres" );
  133. Mcampovazio.setNeutralButton( "OK", null );
  134. Mcampovazio.show();
  135.  
  136. }
  137.  
  138. }
  139.  
  140. private boolean verificaCampovazio(String valor) {
  141.  
  142. Boolean resultado = (TextUtils.isEmpty( valor ) || valor.trim().isEmpty());
  143.  
  144. return resultado;
  145. }
  146.  
  147. private boolean emailValidar(String email) {
  148.  
  149. Boolean resultado = (!verificaCampovazio( email ) && Patterns.EMAIL_ADDRESS.matcher( email ).matches());
  150.  
  151. return resultado;
  152. }
  153.  
  154. private String getMACAddress(String interfaceName) {
  155. try {
  156. List<NetworkInterface> interfaces = Collections.list( NetworkInterface.getNetworkInterfaces() );
  157. for (NetworkInterface intf : interfaces) {
  158. if (interfaceName != null) {
  159. if (!intf.getName().equalsIgnoreCase( interfaceName )) continue;
  160. }
  161. byte[] mac = intf.getHardwareAddress();
  162. if (mac == null) return "";
  163. StringBuilder buf = new StringBuilder();
  164. for (byte aMac : mac) buf.append( String.format( "%02X:", aMac ) );
  165. if (buf.length() > 0) buf.deleteCharAt( buf.length() - 1 );
  166. return buf.toString();
  167. }
  168. } catch (Exception ignored) {
  169. }
  170.  
  171. return "";
  172. }
  173.  
  174. class SolicitaDados extends AsyncTask<String,Void, String>{
  175.  
  176. @Override
  177.  
  178. protected String doInBackground (String... urls){
  179.  
  180. return conexao.postdados (urls[0],parametros);
  181.  
  182. }
  183.  
  184. @Override
  185.  
  186. protected void onPostExecute (String resultado){
  187.  
  188.  
  189. if (resultado.contains("email_erro")){
  190.  
  191. AlertDialog.Builder Mcampovazio = new AlertDialog.Builder( efetuarcadastro.this.getActivity() );
  192. Mcampovazio.setTitle( "Aviso: " );
  193. Mcampovazio.setMessage( "Endereço MAC já cadastrado" );
  194. Mcampovazio.setNeutralButton( "OK", null );
  195. Mcampovazio.show();
  196.  
  197. }
  198.  
  199. else if (resultado.contains("login_erro")){
  200.  
  201. AlertDialog.Builder Mcampovazio = new AlertDialog.Builder( efetuarcadastro.this.getActivity() );
  202. Mcampovazio.setTitle( "Aviso: " );
  203. Mcampovazio.setMessage( "Login já cadastrado" );
  204. Mcampovazio.setNeutralButton( "OK", null );
  205. Mcampovazio.show();
  206.  
  207. }
  208.  
  209. else if (resultado.contains("login_ok")) {
  210.  
  211. FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
  212. ft.replace( R.id.frame_layout, new Tela_Principal() );
  213. ft.commit();
  214. Toast.makeText( getActivity().getApplicationContext(), "Cadastro Realizado com Sucesso", Toast.LENGTH_LONG ).show();
  215. }
  216.  
  217. else
  218.  
  219. {
  220.  
  221. Toast.makeText( getActivity().getApplicationContext(), "Erro ao realizar cadastro", Toast.LENGTH_LONG ).show();
  222.  
  223. }
  224.  
  225. }
  226.  
  227. }
  228.  
  229. } );
  230.  
  231. return myView;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement