Advertisement
Guest User

Nothing

a guest
Oct 24th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.61 KB | None | 0 0
  1. package com.example.adrax.dely;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.util.Log;
  10. import android.util.Patterns;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.google.android.gms.appindexing.Action;
  18. import com.google.android.gms.appindexing.AppIndex;
  19. import com.google.android.gms.common.api.GoogleApiClient;
  20.  
  21. import java.io.BufferedInputStream;
  22. import java.io.BufferedOutputStream;
  23. import java.io.BufferedReader;
  24. import java.io.ByteArrayOutputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.InputStreamReader;
  28. import java.io.OutputStream;
  29. import java.io.OutputStreamWriter;
  30. import java.io.UnsupportedEncodingException;
  31. import java.net.HttpURLConnection;
  32. import java.net.URL;
  33. import java.net.URLConnection;
  34. import java.net.URLEncoder;
  35. import java.util.ArrayList;
  36.  
  37. import butterknife.ButterKnife;
  38. import butterknife.Bind;
  39.  
  40. public class SignupActivity extends AppCompatActivity {
  41.     private static final String TAG = "SignupActivity";
  42.  
  43.     @Bind(R.id.input_name)
  44.     EditText _nameText;
  45.     @Bind(R.id.input_email)
  46.     EditText _emailText;
  47.     @Bind(R.id.input_password)
  48.     EditText _passwordText;
  49.     @Bind(R.id.btn_signup)
  50.     Button _signupButton;
  51.     @Bind(R.id.link_login)
  52.     TextView _loginLink;
  53.     /**
  54.      * ATTENTION: This was auto-generated to implement the App Indexing API.
  55.      * See https://g.co/AppIndexing/AndroidStudio for more information.
  56.      */
  57.     private GoogleApiClient client;
  58.  
  59.     @Override
  60.     public void onCreate(Bundle savedInstanceState) {
  61.         super.onCreate(savedInstanceState);
  62.         setContentView(R.layout.activity_signup);
  63.         ButterKnife.bind(this);
  64.  
  65.  
  66.         _signupButton.setOnClickListener(new View.OnClickListener() {
  67.             @Override
  68.             public void onClick(View v) {
  69.                 try {
  70.                     signup();
  71.                 } catch (UnsupportedEncodingException e) {
  72.                     e.printStackTrace();
  73.                 }
  74.             }
  75.         });
  76.  
  77.         _loginLink.setOnClickListener(new View.OnClickListener() {
  78.             @Override
  79.             public void onClick(View v) {
  80.                 // Finish the registration screen and return to the Login activity
  81.                 finish();
  82.             }
  83.         });
  84.         // ATTENTION: This was auto-generated to implement the App Indexing API.
  85.         // See https://g.co/AppIndexing/AndroidStudio for more information.
  86.         client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
  87.     }
  88.  
  89.     public void signup() throws UnsupportedEncodingException {
  90.         Log.d(TAG, "Signup");
  91.  
  92.         if (!validate()) {
  93.             onSignupFailed();
  94.             return;
  95.         }
  96.  
  97.         _signupButton.setEnabled(false);
  98.  
  99.         final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this,
  100.                 R.style.AppTheme_Dark_Dialog);
  101.         progressDialog.setIndeterminate(true);
  102.         progressDialog.setMessage("Creating Account...");
  103.         progressDialog.show();
  104.  
  105.         String name = _nameText.getText().toString();
  106.         String email = _emailText.getText().toString();
  107.         String password = _passwordText.getText().toString();
  108.  
  109.  
  110.         /*ArrayList data = new ArrayList();
  111.  
  112.         data.add("Username="+name);
  113.         data.add("Password="+password);
  114.         data.add("Name=user");
  115.         data.add("Surname=user2");
  116.         data.add("Midname=user3");
  117.         data.add("Mail="+email);
  118.         data.add("Selnum=999999");
  119.         data.add("About=userness");
  120.         */String data = URLEncoder.encode("Username", "UTF-8")
  121.                 + "=" + URLEncoder.encode(name, "UTF-8");
  122.         data += "&" + URLEncoder.encode(" Password", "UTF-8") + "="
  123.                 + URLEncoder.encode(password, "UTF-8");
  124.         data += "&" + URLEncoder.encode(" Name", "UTF-8") + "=" //!!!!!!!!!!!!!!!!!!!!!!!!!
  125.                 + URLEncoder.encode("User", "UTF-8");
  126.         data += "&" + URLEncoder.encode(" Surname", "UTF-8") + "="
  127.                 + URLEncoder.encode("User2", "UTF-8");
  128.         data += "&" + URLEncoder.encode(" Midname", "UTF-8") + "="
  129.                 + URLEncoder.encode("User3", "UTF-8");
  130.         data += "&" + URLEncoder.encode(" Mail", "UTF-8") + "="
  131.                 + URLEncoder.encode(email, "UTF-8");
  132.         data += "&" + URLEncoder.encode(" Selnum", "UTF-8") + "="
  133.                 + URLEncoder.encode("88005553535", "UTF-8");
  134.  
  135.         data += "&" + URLEncoder.encode(" About", "UTF-8") + "="
  136.                 + URLEncoder.encode("userness", "UTF-8");
  137.  
  138.         String text = "";
  139.         BufferedReader reader = null;
  140.  
  141.         String output="nothing";
  142.  
  143.         try {
  144.             // Defined URL  where to send data
  145.             URL url = new URL("http://adrax.pythonanywhere.com/register");
  146.             //Google's method
  147.             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  148.  
  149.             urlConnection.setDoOutput(true);
  150.             urlConnection.setRequestMethod("POST");
  151.             //urlConnection.setRequestProperty("Content-Type", "text/plain");
  152.             urlConnection.setChunkedStreamingMode(0);
  153.  
  154.             OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
  155.             out.write(data.getBytes());
  156.             out.flush();
  157.  
  158.             //InputStream in = new BufferedInputStream(urlConnection.getInputStream());
  159.             //output = readStream(in);
  160.  
  161.             // Get the server response
  162.  
  163.             /*reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  164.             StringBuilder sb = new StringBuilder();
  165.             String line = null;
  166.  
  167.             // Read Server Response
  168.             while ((line = reader.readLine()) != null) {
  169.                 // Append server response in string
  170.                 sb.append(line + "\n");
  171.  
  172.             }
  173.  
  174.  
  175.             text = sb.toString();*/
  176.  
  177.             urlConnection.disconnect();
  178.  
  179.         } catch (Exception ex) {
  180.  
  181.         } finally {
  182.             try {
  183.  
  184.                 //reader.close();
  185.             } catch (Exception ex) {
  186.             }
  187.         }
  188.  
  189.         // Show response on activity
  190.         //content.setText( text  );
  191.  
  192.         _nameText.setText(output);
  193.         // TODO: Implement your own signup logic here.
  194.  
  195.         new Handler().postDelayed(
  196.                 new Runnable() {
  197.                     public void run() {
  198.                         // On complete call either onSignupSuccess or onSignupFailed
  199.                         // depending on success
  200.                         //onSignupSuccess();
  201.                          onSignupFailed();
  202.                         progressDialog.dismiss();
  203.                     }
  204.                 }, 3000);
  205.     }
  206.  
  207.     private String readStream(InputStream is) throws IOException {
  208.         StringBuilder sb = new StringBuilder();
  209.         BufferedReader r = new BufferedReader(new InputStreamReader(is),1000);
  210.         for (String line = r.readLine(); line != null; line =r.readLine()){
  211.             sb.append(line);
  212.         }
  213.         is.close();
  214.         return sb.toString();
  215.     }
  216.  
  217.  
  218.     public void onSignupSuccess() {
  219.         _signupButton.setEnabled(true);
  220.         setResult(RESULT_OK, null);
  221.         finish();
  222.         Intent myIntent;
  223.         myIntent = new Intent(SignupActivity.this, LoginActivity.class);
  224.         SignupActivity.this.startActivity(myIntent);
  225.     }
  226.  
  227.     public void onSignupFailed() {
  228.         Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
  229.  
  230.         _signupButton.setEnabled(true);
  231.     }
  232.  
  233.     public boolean validate() {
  234.         boolean valid = true;
  235.  
  236.         String name = _nameText.getText().toString();
  237.         String email = _emailText.getText().toString();
  238.         String password = _passwordText.getText().toString();
  239.  
  240.         if (name.isEmpty() || name.length() < 3) {
  241.             _nameText.setError("at least 3 characters");
  242.             valid = false;
  243.         } else {
  244.             _nameText.setError(null);
  245.         }
  246.  
  247.         if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
  248.             _emailText.setError("enter a valid email address");
  249.             valid = false;
  250.         } else {
  251.             _emailText.setError(null);
  252.         }
  253.  
  254.         if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
  255.             _passwordText.setError("between 4 and 10 alphanumeric characters");
  256.             valid = false;
  257.         } else {
  258.             _passwordText.setError(null);
  259.         }
  260.  
  261.         return valid;
  262.     }
  263.  
  264.     @Override
  265.     public void onStart() {
  266.         super.onStart();
  267.  
  268.         // ATTENTION: This was auto-generated to implement the App Indexing API.
  269.         // See https://g.co/AppIndexing/AndroidStudio for more information.
  270.  
  271.  
  272.         client.connect();
  273.         Action viewAction = Action.newAction(
  274.                 Action.TYPE_VIEW, // TODO: choose an action type.
  275.                 "Signup Page", // TODO: Define a title for the content shown.
  276.                 // TODO: If you have web page content that matches this app activity's content,
  277.                 // make sure this auto-generated web page URL is correct.
  278.                 // Otherwise, set the URL to null.
  279.                 Uri.parse("http://host/path"),
  280.                 // TODO: Make sure this auto-generated app URL is correct.
  281.                 Uri.parse("android-app://com.example.adrax.dely/http/host/path")
  282.         );
  283.         AppIndex.AppIndexApi.start(client, viewAction);
  284.     }
  285.  
  286.     @Override
  287.     public void onStop() {
  288.         super.onStop();
  289.  
  290.         // ATTENTION: This was auto-generated to implement the App Indexing API.
  291.         // See https://g.co/AppIndexing/AndroidStudio for more information.
  292.         Action viewAction = Action.newAction(
  293.                 Action.TYPE_VIEW, // TODO: choose an action type.
  294.                 "Signup Page", // TODO: Define a title for the content shown.
  295.                 // TODO: If you have web page content that matches this app activity's content,
  296.                 // make sure this auto-generated web page URL is correct.
  297.                 // Otherwise, set the URL to null.
  298.                 Uri.parse("http://host/path"),
  299.                 // TODO: Make sure this auto-generated app URL is correct.
  300.                 Uri.parse("android-app://com.example.adrax.dely/http/host/path")
  301.         );
  302.         AppIndex.AppIndexApi.end(client, viewAction);
  303.         client.disconnect();
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement