SHARE
TWEET

Untitled

a guest Apr 21st, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.content.Context;
  2. import com.android.volley.Request;
  3. import com.android.volley.RequestQueue;
  4. import com.android.volley.toolbox.Volley;
  5.  
  6. public class AppSingleton {
  7.     private static AppSingleton mAppSingletonInstance;
  8.     private RequestQueue mRequestQueue;
  9.     private static Context mContext;
  10.  
  11. private AppSingleton(Context context) {
  12.     mContext = context;
  13.     mRequestQueue = getRequestQueue();
  14. }
  15.  
  16. public static synchronized AppSingleton getInstance(Context context) {
  17.     if (mAppSingletonInstance == null) {
  18.         mAppSingletonInstance = new AppSingleton(context);
  19.     }
  20.     return mAppSingletonInstance;
  21. }
  22.  
  23. public RequestQueue getRequestQueue() {
  24.     if (mRequestQueue == null) {
  25.         // getApplicationContext() is key, it keeps you from leaking the
  26.         // Activity or BroadcastReceiver if someone passes one in.
  27.         mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
  28.     }
  29.     return mRequestQueue;
  30. }
  31.  
  32. public <T> void addToRequestQueue(Request<T> req,String tag) {
  33.     req.setTag(tag);
  34.     getRequestQueue().add(req);
  35. }
  36. }
  37.    
  38. import android.app.Activity;
  39. import android.app.ProgressDialog;
  40. import android.content.Intent;
  41. import android.os.Bundle;
  42. import android.support.v7.app.AppCompatActivity;
  43. import android.util.Log;
  44. import android.view.View;
  45. import android.widget.Button;
  46. import android.widget.EditText;
  47. import android.widget.RadioGroup;
  48. import android.widget.Toast;
  49. import com.android.volley.Request;
  50. import com.android.volley.Response;
  51. import com.android.volley.VolleyError;
  52. import com.android.volley.toolbox.StringRequest;
  53. import org.json.JSONException;
  54. import org.json.JSONObject;
  55. import java.util.HashMap;
  56. import java.util.Map;
  57.  
  58. public class Main_formulario_docente extends Activity {
  59.  
  60. private static final String TAG = "RegisterActivity";
  61. private static final String URL_FOR_REGISTRATION = "http://192.168.0.110/android_login_v2/register.php";
  62. ProgressDialog progressDialog;
  63.  
  64. private EditText signupInputName, signupInputEmail, signupInputPassword, signupInputPassword2, signupInputAge;
  65. private Button btnSignUp;
  66. private Button btnLinkLogin;
  67. private RadioGroup genderRadioGroup;
  68.  
  69. @Override
  70. protected void onCreate(Bundle savedInstanceState) {
  71.     super.onCreate(savedInstanceState);
  72.     setContentView(R.layout.formulario_docente);
  73.  
  74.     // Progress dialog
  75.     progressDialog = new ProgressDialog(this);
  76.     progressDialog.setCancelable(false);
  77.  
  78.     signupInputName = (EditText) findViewById(R.id.name);
  79.     signupInputEmail = (EditText) findViewById(R.id.email);
  80.     signupInputPassword = (EditText) findViewById(R.id.password);
  81.     signupInputPassword2 = (EditText) findViewById(R.id.password2);
  82.     //signupInputAge = (EditText) findViewById(R.id.signup_input_age);
  83.  
  84.     btnSignUp = (Button) findViewById(R.id.btnRegister);
  85.     btnLinkLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
  86.  
  87.     //genderRadioGroup = (RadioGroup) findViewById(R.id.gender_radio_group);
  88.     btnSignUp.setOnClickListener(new View.OnClickListener() {
  89.         @Override
  90.         public void onClick(View view) {
  91.             submitForm();
  92.         }
  93.     });
  94.     btnLinkLogin.setOnClickListener(new View.OnClickListener() {
  95.         @Override
  96.         public void onClick(View view) {
  97.  
  98.             Intent i = new Intent(getApplicationContext(),Main_docente.class);
  99.             startActivity(i);
  100.             finish();
  101.         }
  102.     });
  103. }
  104.  
  105. private void submitForm() {
  106.     registerUser(signupInputName.getText().toString(),
  107.             signupInputEmail.getText().toString(),
  108.             signupInputPassword.getText().toString());
  109. }
  110.  
  111. private void registerUser(final String name,  final String email, final String password) {
  112.     // Tag used to cancel the request
  113.     String cancel_req_tag = "register";
  114.  
  115.     progressDialog.setMessage("Registrando ...");
  116.     showDialog();
  117.  
  118.     StringRequest strReq = new StringRequest(Request.Method.POST,
  119.             URL_FOR_REGISTRATION, new Response.Listener<String>() {
  120.  
  121.         @Override
  122.         public void onResponse(String response) {
  123.             Log.d(TAG, "Register Response: " + response.toString());
  124.             hideDialog();
  125.  
  126.             try {
  127.                 JSONObject jObj = new JSONObject(response);
  128.                 boolean error = jObj.getBoolean("error");
  129.  
  130.                 if (!error) {
  131.                     String user = jObj.getJSONObject("user").getString("name");
  132.                     Toast.makeText(getApplicationContext(), "Hi " + user +", You are successfully Added!", Toast.LENGTH_SHORT).show();
  133.  
  134.                     // Launch login activity
  135.                     Intent intent = new Intent(
  136.                             Main_formulario_docente.this, Main_docente.class);
  137.                     startActivity(intent);
  138.                     finish();
  139.                 } else {
  140.  
  141.                     String errorMsg = jObj.getString("error_msg");
  142.                     Toast.makeText(getApplicationContext(),
  143.                             errorMsg, Toast.LENGTH_LONG).show();
  144.                 }
  145.             } catch (JSONException e) {
  146.                 e.printStackTrace();
  147.             }
  148.  
  149.         }
  150.     }, new Response.ErrorListener() {
  151.  
  152.         @Override
  153.         public void onErrorResponse(VolleyError error) {
  154.             Log.e(TAG, "Registration Error: " + error.getMessage());
  155.             Toast.makeText(getApplicationContext(),
  156.                     error.getMessage(), Toast.LENGTH_LONG).show();
  157.             hideDialog();
  158.         }
  159.     }) {
  160.         @Override
  161.         protected Map<String, String> getParams() {
  162.             // Posting params to register url
  163.             Map<String, String> params = new HashMap<String, String>();
  164.             params.put("name", name);
  165.             params.put("email", email);
  166.             params.put("password", password);
  167.             //params.put("gender", gender);
  168.             //params.put("age", dob);
  169.             return params;
  170.         }
  171.     };
  172.     // Adding request to request queue
  173.     AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, cancel_req_tag);
  174. }
  175.  
  176. private void showDialog() {
  177.     if (!progressDialog.isShowing())
  178.         progressDialog.show();
  179. }
  180.  
  181. private void hideDialog() {
  182.     if (progressDialog.isShowing())
  183.         progressDialog.dismiss();
  184. }
  185.  
  186. }
  187.    
  188. import android.app.Activity;
  189. import android.app.ProgressDialog;
  190. import android.support.v7.app.AppCompatActivity;
  191. import android.os.Bundle;
  192. import android.util.Log;
  193. import android.widget.Button;
  194. import android.widget.EditText;
  195. import android.content.Intent;
  196. import android.view.View;
  197. import android.widget.Toast;
  198. import com.android.volley.Request;
  199. import com.android.volley.Response;
  200. import com.android.volley.VolleyError;
  201. import com.android.volley.toolbox.StringRequest;
  202. import org.json.JSONException;
  203. import org.json.JSONObject;
  204. import java.util.HashMap;
  205. import java.util.Map;
  206.  
  207.  
  208. public class Main_docente extends Activity {
  209. private static final String TAG = "LoginActivity";
  210. private static final String URL_FOR_LOGIN = "http://192.168.0.110/android_login_v2/login.php";
  211. ProgressDialog progressDialog;
  212. private EditText loginInputEmail, loginInputPassword;
  213. private Button btnlogin;
  214. private Button btnLinkSignup;
  215.  
  216. @Override
  217. protected void onCreate(Bundle savedInstanceState) {
  218.     super.onCreate(savedInstanceState);
  219.     setContentView(R.layout.screen_docente);
  220.  
  221.  
  222.     loginInputEmail = (EditText) findViewById(R.id.email);
  223.     loginInputPassword = (EditText) findViewById(R.id.password);
  224.     btnlogin = (Button) findViewById(R.id.btnLogin);
  225.     btnLinkSignup = (Button) findViewById(R.id.btnLinkToRegisterScreen);
  226.     // Progress dialog
  227.     progressDialog = new ProgressDialog(this);
  228.     progressDialog.setCancelable(false);
  229.  
  230.     btnlogin.setOnClickListener(new View.OnClickListener() {
  231.         @Override
  232.         public void onClick(View view) {
  233.             loginUser(loginInputEmail.getText().toString(), loginInputPassword.getText().toString());
  234.         }
  235.     });
  236.  
  237.     btnLinkSignup.setOnClickListener(new View.OnClickListener() {
  238.         @Override
  239.         public void onClick(View view) {
  240.             Intent i = new Intent(getApplicationContext(), Main_formulario_docente.class);
  241.             startActivity(i);
  242.         }
  243.     });
  244. }
  245.  
  246. private void loginUser( final String email, final String password) {
  247.     // Tag used to cancel the request
  248.     String cancel_req_tag = "login";
  249.     progressDialog.setMessage("Logging you in...");
  250.     showDialog();
  251.     StringRequest strReq = new StringRequest(Request.Method.POST,
  252.             URL_FOR_LOGIN, new Response.Listener<String>() {
  253.  
  254.         @Override
  255.         public void onResponse(String response) {
  256.             Log.d(TAG, "Register Response: " + response.toString());
  257.             hideDialog();
  258.             try {
  259.                 JSONObject jObj = new JSONObject(response);
  260.                 boolean error = jObj.getBoolean("error");
  261.  
  262.                 if (!error) {
  263.                     String user = jObj.getJSONObject("user").getString("name");
  264.                     // Launch User activity
  265.                     Intent intent = new Intent(
  266.                             Main_docente.this, MainActivity.class);
  267.                     intent.putExtra("username", user);
  268.                     startActivity(intent);
  269.                     finish();
  270.                 } else {
  271.  
  272.                     String errorMsg = jObj.getString("error_msg");
  273.                     Toast.makeText(getApplicationContext(),
  274.                             errorMsg, Toast.LENGTH_LONG).show();
  275.                 }
  276.             } catch (JSONException e) {
  277.                 e.printStackTrace();
  278.             }
  279.  
  280.         }
  281.     }, new Response.ErrorListener() {
  282.  
  283.         @Override
  284.         public void onErrorResponse(VolleyError error) {
  285.             Log.e(TAG, "Login Error: " + error.getMessage());
  286.             Toast.makeText(getApplicationContext(),
  287.                     error.getMessage(), Toast.LENGTH_LONG).show();
  288.             hideDialog();
  289.         }
  290.     }) {
  291.  
  292.         @Override
  293.         protected Map<String, String> getParams() {
  294.             // Posting params to login url
  295.             Map<String, String> params = new HashMap<String, String>();
  296.             params.put("email", email);
  297.             params.put("password", password);
  298.             return params;
  299.         }
  300.  
  301.     };
  302.     // Adding request to request queue
  303.     AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq,cancel_req_tag);
  304. }
  305.  
  306. private void showDialog() {
  307.     if (!progressDialog.isShowing())
  308.         progressDialog.show();
  309. }
  310. private void hideDialog() {
  311.     if (progressDialog.isShowing())
  312.         progressDialog.dismiss();
  313. }
  314.  
  315. }
  316.    
  317. <?php
  318.  
  319. define("DB_HOST", "localhost");
  320. define("DB_USER", "root");
  321. define("DB_PASSWORD", "12345");
  322. define("DB_DATABASE", "android_login");
  323.  
  324. ?>
  325.    
  326. <?php
  327. class android_login_connect {
  328. private $conn;
  329.  
  330. // Connecting to database
  331. public function connect() {
  332.     require_once 'android_login_config.php';
  333.  
  334.     // Connecting to mysql database
  335.     $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
  336.     // return database object
  337.     return $this->conn;
  338. }
  339. }
  340.  
  341. ?>
  342.    
  343. <?php
  344. require_once 'update_user_info.php';
  345. $db = new update_user_info();
  346.  
  347. // json response array
  348. $response = array("error" => FALSE);
  349.  
  350. if (isset($_POST['email']) && isset($_POST['password'])) {
  351.  
  352. // receiving the post params
  353. $email = $_POST['email'];
  354. $password = $_POST['password'];
  355.  
  356. // get the user by email and password
  357. $user = $db->VerifyUserAuthentication($email, $password);
  358.  
  359. if ($user != false) {
  360.     // use is found
  361.     $response["error"] = FALSE;
  362.     $response["user"]["name"] = $user["name"];
  363.     $response["user"]["email"] = $user["email"];
  364.     echo json_encode($response);
  365. } else {
  366.     // user is not found with the credentials
  367.     $response["error"] = TRUE;
  368.     $response["error_msg"] = "Login credentials are wrong. Please try
  369. again!";
  370.     echo json_encode($response);
  371. }
  372. } else {
  373. // required post params is missing
  374. $response["error"] = TRUE;
  375. $response["error_msg"] = "Required parameters email or password is
  376. missing!";
  377. echo json_encode($response);
  378. }
  379. ?>
  380.    
  381. <?php
  382.  
  383. require_once 'update_user_info.php';
  384. $db = new update_user_info();
  385.  
  386. // json response array
  387. $response = array("error" => FALSE);
  388.  
  389. if (isset($_POST['name']) && isset($_POST['email']) &&
  390. isset($_POST['password'])) {
  391.  
  392. // receiving the post params
  393. $name = $_POST['name'];
  394. $email = $_POST['email'];
  395. $password = $_POST['password'];
  396.  
  397. // check if user is already existed with the same email
  398. if ($db->CheckExistingUser($email)) {
  399.     // user already existed
  400.     $response["error"] = TRUE;
  401.     $response["error_msg"] = "User already existed with " . $email;
  402.     echo json_encode($response);
  403. } else {
  404.     // create a new user
  405.     $user = $db->StoreUserInfo($name, $email, $password);
  406.     if ($user) {
  407.         // user stored successfully
  408.         $response["error"] = FALSE;
  409.         $response["user"]["name"] = $user["name"];
  410.         $response["user"]["email"] = $user["email"];
  411.         echo json_encode($response);
  412.     } else {
  413.         // user failed to store
  414.         $response["error"] = TRUE;
  415.         $response["error_msg"] = "Unknown error occurred in registration!";
  416.         echo json_encode($response);
  417.     }
  418. }
  419. } else {
  420. $response["error"] = TRUE;
  421. $response["error_msg"] = "Required parameters (name, email or password) is
  422. missing!";
  423. echo json_encode($response);
  424. }
  425. ?>
  426.    
  427. <?php
  428.  
  429. class update_user_info {
  430.  
  431. private $conn;
  432.  
  433. // constructor
  434. function __construct() {
  435.     require_once 'android_login_connect.php';
  436.     // connecting to database
  437.     $db = new android_login_connect();
  438.     $this->conn = $db->connect();
  439. }
  440.  
  441. // destructor
  442. function __destruct() {
  443.  
  444. }
  445.  
  446. /**
  447.  * Storing new user
  448.  * returns user details
  449.  */
  450. public function StoreUserInfo($name, $email, $password) {
  451.     $hash = $this->hashFunction($password);
  452.     $encrypted_password = $hash["encrypted"]; // encrypted password
  453.     $salt = $hash["salt"]; // salt
  454.  
  455.     $stmt = $this->conn->prepare("INSERT INTO android_php_post(name, email,
  456. encrypted_password, salt) VALUES(?, ?, ?, ?)");
  457.     $stmt->bind_param("ssssss", $name, $email, $encrypted_password, $salt);
  458.     $result = $stmt->execute();
  459.     $stmt->close();
  460.  
  461.     // check for successful store
  462.     if ($result) {
  463.         $stmt = $this->conn->prepare("SELECT name, email,
  464. encrypted_password, salt FROM android_php_post WHERE email = ?");
  465.         $stmt->bind_param("s", $email);
  466.         $stmt->execute();
  467.         $stmt-> bind_result($token2,$token3,$token4,$token5);
  468.         while ( $stmt-> fetch() ) {
  469.            $user["name"] = $token2;
  470.            $user["email"] = $token3;
  471.         }
  472.         $stmt->close();
  473.         return $user;
  474.     } else {
  475.       return false;
  476.     }
  477. }
  478.  
  479. /**
  480.  * Get user by email and password
  481.  */
  482. public function VerifyUserAuthentication($email, $password) {
  483.  
  484.     $stmt = $this->conn->prepare("SELECT name, email, encrypted_password,
  485. salt FROM android_php_post WHERE email = ?");
  486.  
  487.     $stmt->bind_param("s", $email);
  488.  
  489.     if ($stmt->execute()) {
  490.         $stmt-> bind_result($token2,$token3,$token4,$token5);
  491.  
  492.         while ( $stmt-> fetch() ) {
  493.            $user["name"] = $token2;
  494.            $user["email"] = $token3;
  495.            $user["encrypted_password"] = $token4;
  496.            $user["salt"] = $token5;
  497.         }
  498.  
  499.         $stmt->close();
  500.  
  501.         // verifying user password
  502.         $salt = $token5;
  503.         $encrypted_password = $token4;
  504.         $hash = $this->CheckHashFunction($salt, $password);
  505.         // check for password equality
  506.         if ($encrypted_password == $hash) {
  507.             // user authentication details are correct
  508.             return $user;
  509.         }
  510.     } else {
  511.         return NULL;
  512.     }
  513. }
  514.  
  515. /**
  516.  * Check user is existed or not
  517.  */
  518. public function CheckExistingUser($email) {
  519.     $stmt = $this->conn->prepare("SELECT email from android_php_post WHERE
  520. email = ?");
  521.  
  522.     $stmt->bind_param("s", $email);
  523.  
  524.     $stmt->execute();
  525.  
  526.     $stmt->store_result();
  527.  
  528.     if ($stmt->num_rows > 0) {
  529.         // user existed
  530.         $stmt->close();
  531.         return true;
  532.     } else {
  533.         // user not existed
  534.         $stmt->close();
  535.         return false;
  536.     }
  537. }
  538.  
  539. /**
  540.  * Encrypting password
  541.  * @param password
  542.  * returns salt and encrypted password
  543.  */
  544. public function hashFunction($password) {
  545.  
  546.     $salt = sha1(rand());
  547.     $salt = substr($salt, 0, 10);
  548.     $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
  549.     $hash = array("salt" => $salt, "encrypted" => $encrypted);
  550.     return $hash;
  551. }
  552.  
  553. /**
  554.  * Decrypting password
  555.  * @param salt, password
  556.  * returns hash string
  557.  */
  558. public function checkHashFunction($salt, $password) {
  559.     $hash = base64_encode(sha1($password . $salt, true) . $salt);
  560.     return $hash;
  561. }
  562.  
  563. }
  564.  
  565. ?>
  566.    
  567. 04-21 08:27:41.036 24791-24791/joinder.proyecto W/System.err:
  568. org.json.JSONException: Value <br of type java.lang.String cannot be
  569. converted to JSONObject
  570. 04-21 08:27:41.041 24791-24791/joinder.proyecto W/System.err:     at
  571. org.json.JSON.typeMismatch(JSON.java:111)
  572. 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err:     at
  573. org.json.JSONObject.<init>(JSONObject.java:160)
  574. 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err:     at
  575. org.json.JSONObject.<init>(JSONObject.java:173)
  576. 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err:     at
  577. joinder.proyecto.Main_formulario_docente$3.onResponse
  578. (Main_formulario_docente.j
  579. ava:93)
  580. 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err:     at  
  581. joinder.proyecto.Main_formulario_docente$3.onResponse
  582. (Main_formulario_docente.java:85)
  583. 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err:     at
  584. com.android.volley.toolbox.StringRequest.deliverResponse
  585. (StringRequest.java:60)
  586. 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err:     at
  587. com.android.volley.toolbox.StringRequest.deliverResponse
  588. (StringRequest.java:30)
  589. 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err:     at
  590. com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run
  591. (ExecutorDelivery.java:99)
  592. 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err:     at
  593. android.os.Handler.handleCallback(Handler.java:739)
  594. 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err:     at
  595. android.os.Handler.dispatchMessage(Handler.java:95)
  596. 04-21 08:27:41.042 24791-24791/joinder.proyecto W/System.err:     at
  597. android.os.Looper.loop(Looper.java:135)
  598.    
  599. @Override
  600.         public void onResponse(String response) {
  601.    
  602. JSONObject jObj = new JSONObject(response);
RAW Paste Data
Top