Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.adrax.dely;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.util.Patterns;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.appindexing.Action;
- import com.google.android.gms.appindexing.AppIndex;
- import com.google.android.gms.common.api.GoogleApiClient;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import butterknife.ButterKnife;
- import butterknife.Bind;
- public class SignupActivity extends AppCompatActivity {
- private static final String TAG = "SignupActivity";
- @Bind(R.id.input_name)
- EditText _nameText;
- @Bind(R.id.input_email)
- EditText _emailText;
- @Bind(R.id.input_password)
- EditText _passwordText;
- @Bind(R.id.btn_signup)
- Button _signupButton;
- @Bind(R.id.link_login)
- TextView _loginLink;
- /**
- * ATTENTION: This was auto-generated to implement the App Indexing API.
- * See https://g.co/AppIndexing/AndroidStudio for more information.
- */
- private GoogleApiClient client;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_signup);
- ButterKnife.bind(this);
- _signupButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- signup();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- });
- _loginLink.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // Finish the registration screen and return to the Login activity
- finish();
- }
- });
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
- }
- public void signup() throws UnsupportedEncodingException {
- Log.d(TAG, "Signup");
- if (!validate()) {
- onSignupFailed();
- return;
- }
- _signupButton.setEnabled(false);
- final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this,
- R.style.AppTheme_Dark_Dialog);
- progressDialog.setIndeterminate(true);
- progressDialog.setMessage("Creating Account...");
- progressDialog.show();
- String name = _nameText.getText().toString();
- String email = _emailText.getText().toString();
- String password = _passwordText.getText().toString();
- /*ArrayList data = new ArrayList();
- data.add("Username="+name);
- data.add("Password="+password);
- data.add("Name=user");
- data.add("Surname=user2");
- data.add("Midname=user3");
- data.add("Mail="+email);
- data.add("Selnum=999999");
- data.add("About=userness");
- */String data = URLEncoder.encode("Username", "UTF-8")
- + "=" + URLEncoder.encode(name, "UTF-8");
- data += "&" + URLEncoder.encode(" Password", "UTF-8") + "="
- + URLEncoder.encode(password, "UTF-8");
- data += "&" + URLEncoder.encode(" Name", "UTF-8") + "=" //!!!!!!!!!!!!!!!!!!!!!!!!!
- + URLEncoder.encode("User", "UTF-8");
- data += "&" + URLEncoder.encode(" Surname", "UTF-8") + "="
- + URLEncoder.encode("User2", "UTF-8");
- data += "&" + URLEncoder.encode(" Midname", "UTF-8") + "="
- + URLEncoder.encode("User3", "UTF-8");
- data += "&" + URLEncoder.encode(" Mail", "UTF-8") + "="
- + URLEncoder.encode(email, "UTF-8");
- data += "&" + URLEncoder.encode(" Selnum", "UTF-8") + "="
- + URLEncoder.encode("88005553535", "UTF-8");
- data += "&" + URLEncoder.encode(" About", "UTF-8") + "="
- + URLEncoder.encode("userness", "UTF-8");
- String text = "";
- BufferedReader reader = null;
- String output="nothing";
- try {
- // Defined URL where to send data
- URL url = new URL("http://adrax.pythonanywhere.com/register");
- //Google's method
- HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
- urlConnection.setDoOutput(true);
- urlConnection.setRequestMethod("POST");
- //urlConnection.setRequestProperty("Content-Type", "text/plain");
- urlConnection.setChunkedStreamingMode(0);
- OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
- out.write(data.getBytes());
- out.flush();
- //InputStream in = new BufferedInputStream(urlConnection.getInputStream());
- //output = readStream(in);
- // Get the server response
- /*reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
- StringBuilder sb = new StringBuilder();
- String line = null;
- // Read Server Response
- while ((line = reader.readLine()) != null) {
- // Append server response in string
- sb.append(line + "\n");
- }
- text = sb.toString();*/
- urlConnection.disconnect();
- } catch (Exception ex) {
- } finally {
- try {
- //reader.close();
- } catch (Exception ex) {
- }
- }
- // Show response on activity
- //content.setText( text );
- _nameText.setText(output);
- // TODO: Implement your own signup logic here.
- new Handler().postDelayed(
- new Runnable() {
- public void run() {
- // On complete call either onSignupSuccess or onSignupFailed
- // depending on success
- //onSignupSuccess();
- onSignupFailed();
- progressDialog.dismiss();
- }
- }, 3000);
- }
- private String readStream(InputStream is) throws IOException {
- StringBuilder sb = new StringBuilder();
- BufferedReader r = new BufferedReader(new InputStreamReader(is),1000);
- for (String line = r.readLine(); line != null; line =r.readLine()){
- sb.append(line);
- }
- is.close();
- return sb.toString();
- }
- public void onSignupSuccess() {
- _signupButton.setEnabled(true);
- setResult(RESULT_OK, null);
- finish();
- Intent myIntent;
- myIntent = new Intent(SignupActivity.this, LoginActivity.class);
- SignupActivity.this.startActivity(myIntent);
- }
- public void onSignupFailed() {
- Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
- _signupButton.setEnabled(true);
- }
- public boolean validate() {
- boolean valid = true;
- String name = _nameText.getText().toString();
- String email = _emailText.getText().toString();
- String password = _passwordText.getText().toString();
- if (name.isEmpty() || name.length() < 3) {
- _nameText.setError("at least 3 characters");
- valid = false;
- } else {
- _nameText.setError(null);
- }
- if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
- _emailText.setError("enter a valid email address");
- valid = false;
- } else {
- _emailText.setError(null);
- }
- if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
- _passwordText.setError("between 4 and 10 alphanumeric characters");
- valid = false;
- } else {
- _passwordText.setError(null);
- }
- return valid;
- }
- @Override
- public void onStart() {
- super.onStart();
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- client.connect();
- Action viewAction = Action.newAction(
- Action.TYPE_VIEW, // TODO: choose an action type.
- "Signup Page", // TODO: Define a title for the content shown.
- // TODO: If you have web page content that matches this app activity's content,
- // make sure this auto-generated web page URL is correct.
- // Otherwise, set the URL to null.
- Uri.parse("http://host/path"),
- // TODO: Make sure this auto-generated app URL is correct.
- Uri.parse("android-app://com.example.adrax.dely/http/host/path")
- );
- AppIndex.AppIndexApi.start(client, viewAction);
- }
- @Override
- public void onStop() {
- super.onStop();
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- Action viewAction = Action.newAction(
- Action.TYPE_VIEW, // TODO: choose an action type.
- "Signup Page", // TODO: Define a title for the content shown.
- // TODO: If you have web page content that matches this app activity's content,
- // make sure this auto-generated web page URL is correct.
- // Otherwise, set the URL to null.
- Uri.parse("http://host/path"),
- // TODO: Make sure this auto-generated app URL is correct.
- Uri.parse("android-app://com.example.adrax.dely/http/host/path")
- );
- AppIndex.AppIndexApi.end(client, viewAction);
- client.disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement