Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.toolbox.Volley;
- public class AppSingleton {
- private static AppSingleton mAppSingletonInstance;
- private RequestQueue mRequestQueue;
- private static Context mContext;
- private AppSingleton(Context context) {
- mContext = context;
- mRequestQueue = getRequestQueue();
- }
- public static synchronized AppSingleton getInstance(Context context) {
- if (mAppSingletonInstance == null) {
- mAppSingletonInstance = new AppSingleton(context);
- }
- return mAppSingletonInstance;
- }
- public RequestQueue getRequestQueue() {
- if (mRequestQueue == null) {
- // getApplicationContext() is key, it keeps you from leaking the
- // Activity or BroadcastReceiver if someone passes one in.
- mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
- }
- return mRequestQueue;
- }
- public <T> void addToRequestQueue(Request<T> req,String tag) {
- req.setTag(tag);
- getRequestQueue().add(req);
- }
- }
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- import com.android.volley.Request;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.HashMap;
- import java.util.Map;
- public class Main_formulario_docente extends Activity {
- private static final String TAG = "RegisterActivity";
- private static final String URL_FOR_REGISTRATION = "http://192.168.0.110/android_login_v2/register.php";
- ProgressDialog progressDialog;
- private EditText signupInputName, signupInputEmail, signupInputPassword, signupInputPassword2, signupInputAge;
- private Button btnSignUp;
- private Button btnLinkLogin;
- private RadioGroup genderRadioGroup;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.formulario_docente);
- // Progress dialog
- progressDialog = new ProgressDialog(this);
- progressDialog.setCancelable(false);
- signupInputName = (EditText) findViewById(R.id.name);
- signupInputEmail = (EditText) findViewById(R.id.email);
- signupInputPassword = (EditText) findViewById(R.id.password);
- signupInputPassword2 = (EditText) findViewById(R.id.password2);
- //signupInputAge = (EditText) findViewById(R.id.signup_input_age);
- btnSignUp = (Button) findViewById(R.id.btnRegister);
- btnLinkLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
- //genderRadioGroup = (RadioGroup) findViewById(R.id.gender_radio_group);
- btnSignUp.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- submitForm();
- }
- });
- btnLinkLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent i = new Intent(getApplicationContext(),Main_docente.class);
- startActivity(i);
- finish();
- }
- });
- }
- private void submitForm() {
- registerUser(signupInputName.getText().toString(),
- signupInputEmail.getText().toString(),
- signupInputPassword.getText().toString());
- }
- private void registerUser(final String name, final String email, final String password) {
- // Tag used to cancel the request
- String cancel_req_tag = "register";
- progressDialog.setMessage("Registrando ...");
- showDialog();
- StringRequest strReq = new StringRequest(Request.Method.POST,
- URL_FOR_REGISTRATION, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.d(TAG, "Register Response: " + response.toString());
- hideDialog();
- try {
- JSONObject jObj = new JSONObject(response);
- boolean error = jObj.getBoolean("error");
- if (!error) {
- String user = jObj.getJSONObject("user").getString("name");
- Toast.makeText(getApplicationContext(), "Hi " + user +", You are successfully Added!", Toast.LENGTH_SHORT).show();
- // Launch login activity
- Intent intent = new Intent(
- Main_formulario_docente.this, Main_docente.class);
- startActivity(intent);
- finish();
- } else {
- String errorMsg = jObj.getString("error_msg");
- Toast.makeText(getApplicationContext(),
- errorMsg, Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.e(TAG, "Registration Error: " + error.getMessage());
- Toast.makeText(getApplicationContext(),
- error.getMessage(), Toast.LENGTH_LONG).show();
- hideDialog();
- }
- }) {
- @Override
- protected Map<String, String> getParams() {
- // Posting params to register url
- Map<String, String> params = new HashMap<String, String>();
- params.put("name", name);
- params.put("email", email);
- params.put("password", password);
- //params.put("gender", gender);
- //params.put("age", dob);
- return params;
- }
- };
- // Adding request to request queue
- AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, cancel_req_tag);
- }
- private void showDialog() {
- if (!progressDialog.isShowing())
- progressDialog.show();
- }
- private void hideDialog() {
- if (progressDialog.isShowing())
- progressDialog.dismiss();
- }
- }
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Button;
- import android.widget.EditText;
- import android.content.Intent;
- import android.view.View;
- import android.widget.Toast;
- import com.android.volley.Request;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.HashMap;
- import java.util.Map;
- public class Main_docente extends Activity {
- private static final String TAG = "LoginActivity";
- private static final String URL_FOR_LOGIN = "http://192.168.0.110/android_login_v2/login.php";
- ProgressDialog progressDialog;
- private EditText loginInputEmail, loginInputPassword;
- private Button btnlogin;
- private Button btnLinkSignup;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.screen_docente);
- loginInputEmail = (EditText) findViewById(R.id.email);
- loginInputPassword = (EditText) findViewById(R.id.password);
- btnlogin = (Button) findViewById(R.id.btnLogin);
- btnLinkSignup = (Button) findViewById(R.id.btnLinkToRegisterScreen);
- // Progress dialog
- progressDialog = new ProgressDialog(this);
- progressDialog.setCancelable(false);
- btnlogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- loginUser(loginInputEmail.getText().toString(), loginInputPassword.getText().toString());
- }
- });
- btnLinkSignup.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent i = new Intent(getApplicationContext(), Main_formulario_docente.class);
- startActivity(i);
- }
- });
- }
- private void loginUser( final String email, final String password) {
- // Tag used to cancel the request
- String cancel_req_tag = "login";
- progressDialog.setMessage("Logging you in...");
- showDialog();
- StringRequest strReq = new StringRequest(Request.Method.POST,
- URL_FOR_LOGIN, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.d(TAG, "Register Response: " + response.toString());
- hideDialog();
- try {
- JSONObject jObj = new JSONObject(response);
- boolean error = jObj.getBoolean("error");
- if (!error) {
- String user = jObj.getJSONObject("user").getString("name");
- // Launch User activity
- Intent intent = new Intent(
- Main_docente.this, MainActivity.class);
- intent.putExtra("username", user);
- startActivity(intent);
- finish();
- } else {
- String errorMsg = jObj.getString("error_msg");
- Toast.makeText(getApplicationContext(),
- errorMsg, Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.e(TAG, "Login Error: " + error.getMessage());
- Toast.makeText(getApplicationContext(),
- error.getMessage(), Toast.LENGTH_LONG).show();
- hideDialog();
- }
- }) {
- @Override
- protected Map<String, String> getParams() {
- // Posting params to login url
- Map<String, String> params = new HashMap<String, String>();
- params.put("email", email);
- params.put("password", password);
- return params;
- }
- };
- // Adding request to request queue
- AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq,cancel_req_tag);
- }
- private void showDialog() {
- if (!progressDialog.isShowing())
- progressDialog.show();
- }
- private void hideDialog() {
- if (progressDialog.isShowing())
- progressDialog.dismiss();
- }
- }
- <?php
- define("DB_HOST", "localhost");
- define("DB_USER", "root");
- define("DB_PASSWORD", "12345");
- define("DB_DATABASE", "android_login");
- ?>
- <?php
- class android_login_connect {
- private $conn;
- // Connecting to database
- public function connect() {
- require_once 'android_login_config.php';
- // Connecting to mysql database
- $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
- // return database object
- return $this->conn;
- }
- }
- ?>
- <?php
- require_once 'update_user_info.php';
- $db = new update_user_info();
- // json response array
- $response = array("error" => FALSE);
- if (isset($_POST['email']) && isset($_POST['password'])) {
- // receiving the post params
- $email = $_POST['email'];
- $password = $_POST['password'];
- // get the user by email and password
- $user = $db->VerifyUserAuthentication($email, $password);
- if ($user != false) {
- // use is found
- $response["error"] = FALSE;
- $response["user"]["name"] = $user["name"];
- $response["user"]["email"] = $user["email"];
- echo json_encode($response);
- } else {
- // user is not found with the credentials
- $response["error"] = TRUE;
- $response["error_msg"] = "Login credentials are wrong. Please try
- again!";
- echo json_encode($response);
- }
- } else {
- // required post params is missing
- $response["error"] = TRUE;
- $response["error_msg"] = "Required parameters email or password is
- missing!";
- echo json_encode($response);
- }
- ?>
- <?php
- require_once 'update_user_info.php';
- $db = new update_user_info();
- // json response array
- $response = array("error" => FALSE);
- if (isset($_POST['name']) && isset($_POST['email']) &&
- isset($_POST['password'])) {
- // receiving the post params
- $name = $_POST['name'];
- $email = $_POST['email'];
- $password = $_POST['password'];
- // check if user is already existed with the same email
- if ($db->CheckExistingUser($email)) {
- // user already existed
- $response["error"] = TRUE;
- $response["error_msg"] = "User already existed with " . $email;
- echo json_encode($response);
- } else {
- // create a new user
- $user = $db->StoreUserInfo($name, $email, $password);
- if ($user) {
- // user stored successfully
- $response["error"] = FALSE;
- $response["user"]["name"] = $user["name"];
- $response["user"]["email"] = $user["email"];
- echo json_encode($response);
- } else {
- // user failed to store
- $response["error"] = TRUE;
- $response["error_msg"] = "Unknown error occurred in registration!";
- echo json_encode($response);
- }
- }
- } else {
- $response["error"] = TRUE;
- $response["error_msg"] = "Required parameters (name, email or password) is
- missing!";
- echo json_encode($response);
- }
- ?>
- <?php
- class update_user_info {
- private $conn;
- // constructor
- function __construct() {
- require_once 'android_login_connect.php';
- // connecting to database
- $db = new android_login_connect();
- $this->conn = $db->connect();
- }
- // destructor
- function __destruct() {
- }
- /**
- * Storing new user
- * returns user details
- */
- public function StoreUserInfo($name, $email, $password) {
- $hash = $this->hashFunction($password);
- $encrypted_password = $hash["encrypted"]; // encrypted password
- $salt = $hash["salt"]; // salt
- $stmt = $this->conn->prepare("INSERT INTO android_php_post(name, email,
- encrypted_password, salt) VALUES(?, ?, ?, ?)");
- $stmt->bind_param("ssssss", $name, $email, $encrypted_password, $salt);
- $result = $stmt->execute();
- $stmt->close();
- // check for successful store
- if ($result) {
- $stmt = $this->conn->prepare("SELECT name, email,
- encrypted_password, salt FROM android_php_post WHERE email = ?");
- $stmt->bind_param("s", $email);
- $stmt->execute();
- $stmt-> bind_result($token2,$token3,$token4,$token5);
- while ( $stmt-> fetch() ) {
- $user["name"] = $token2;
- $user["email"] = $token3;
- }
- $stmt->close();
- return $user;
- } else {
- return false;
- }
- }
- /**
- * Get user by email and password
- */
- public function VerifyUserAuthentication($email, $password) {
- $stmt = $this->conn->prepare("SELECT name, email, encrypted_password,
- salt FROM android_php_post WHERE email = ?");
- $stmt->bind_param("s", $email);
- if ($stmt->execute()) {
- $stmt-> bind_result($token2,$token3,$token4,$token5);
- while ( $stmt-> fetch() ) {
- $user["name"] = $token2;
- $user["email"] = $token3;
- $user["encrypted_password"] = $token4;
- $user["salt"] = $token5;
- }
- $stmt->close();
- // verifying user password
- $salt = $token5;
- $encrypted_password = $token4;
- $hash = $this->CheckHashFunction($salt, $password);
- // check for password equality
- if ($encrypted_password == $hash) {
- // user authentication details are correct
- return $user;
- }
- } else {
- return NULL;
- }
- }
- /**
- * Check user is existed or not
- */
- public function CheckExistingUser($email) {
- $stmt = $this->conn->prepare("SELECT email from android_php_post WHERE
- email = ?");
- $stmt->bind_param("s", $email);
- $stmt->execute();
- $stmt->store_result();
- if ($stmt->num_rows > 0) {
- // user existed
- $stmt->close();
- return true;
- } else {
- // user not existed
- $stmt->close();
- return false;
- }
- }
- /**
- * Encrypting password
- * @param password
- * returns salt and encrypted password
- */
- public function hashFunction($password) {
- $salt = sha1(rand());
- $salt = substr($salt, 0, 10);
- $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
- $hash = array("salt" => $salt, "encrypted" => $encrypted);
- return $hash;
- }
- /**
- * Decrypting password
- * @param salt, password
- * returns hash string
- */
- public function checkHashFunction($salt, $password) {
- $hash = base64_encode(sha1($password . $salt, true) . $salt);
- return $hash;
- }
- }
- ?>
- 04-21 08:27:41.036 24791-24791/joinder.proyecto W/System.err:
- org.json.JSONException: Value <br of type java.lang.String cannot be
- converted to JSONObject
- 04-21 08:27:41.041 24791-24791/joinder.proyecto W/System.err: at
- org.json.JSON.typeMismatch(JSON.java:111)
- 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err: at
- org.json.JSONObject.<init>(JSONObject.java:160)
- 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err: at
- org.json.JSONObject.<init>(JSONObject.java:173)
- 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err: at
- joinder.proyecto.Main_formulario_docente$3.onResponse
- (Main_formulario_docente.j
- ava:93)
- 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err: at
- joinder.proyecto.Main_formulario_docente$3.onResponse
- (Main_formulario_docente.java:85)
- 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err: at
- com.android.volley.toolbox.StringRequest.deliverResponse
- (StringRequest.java:60)
- 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err: at
- com.android.volley.toolbox.StringRequest.deliverResponse
- (StringRequest.java:30)
- 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err: at
- com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run
- (ExecutorDelivery.java:99)
- 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err: at
- android.os.Handler.handleCallback(Handler.java:739)
- 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err: at
- android.os.Handler.dispatchMessage(Handler.java:95)
- 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err: at
- android.os.Looper.loop(Looper.java:135)
- @Override
- public void onResponse(String response) {
- JSONObject jObj = new JSONObject(response);
Add Comment
Please, Sign In to add comment