title: Android application login and registration error
body: <p>I have been following a tutorial on developing an android Login and registration System with PHP, MySQL. When registering a user, a record is added to my local database (xampp), but the application crashes. I have looked at similar questions regarding this issue, but I am still confused and can't find a solution to it.</p>
  
  <p>logcat:</p>
  
  <pre><code>12-24 14:43:10.875: E/JSON(782): &lt;br /&gt;
  12-24 14:43:10.875: E/JSON(782): &lt;b&gt;Deprecated&lt;/b&gt;:  mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in &lt;b&gt;G:\xampp\htdocs\learn2crack_login_api\include\DB_Connect.php&lt;/b&gt; on line &lt;b&gt;18&lt;/b&gt;&lt;br /&gt;
  12-24 14:43:10.875: E/JSON(782): {"tag":"register","success":1,"error":0,"user":{"fname":"benmid","lname":"muli","email":"benmid@yahoo.com","uname":"benmid14","uid":"549ad0fcaf8052.70050491","created_at":"2014-12-24 14:43:08"}}
  12-24 14:43:10.875: E/JSON Parser(782): Error parsing data org.json.JSONException: Value &lt;br of type java.lang.String cannot be converted to JSONObject
  12-24 14:43:10.905: D/AndroidRuntime(782): Shutting down VM
  12-24 14:43:10.905: W/dalvikvm(782): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
  12-24 14:43:10.915: E/AndroidRuntime(782): FATAL EXCEPTION: main
  12-24 14:43:10.915: E/AndroidRuntime(782): java.lang.NullPointerException
  12-24 14:43:10.915: E/AndroidRuntime(782):  at com.learn2crack.Register$ProcessRegister.onPostExecute(Register.java:237)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at com.learn2crack.Register$ProcessRegister.onPostExecute(Register.java:1)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at android.os.AsyncTask.finish(AsyncTask.java:631)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at android.os.Handler.dispatchMessage(Handler.java:99)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at android.os.Looper.loop(Looper.java:137)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at android.app.ActivityThread.main(ActivityThread.java:5041)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at java.lang.reflect.Method.invokeNative(Native Method)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at java.lang.reflect.Method.invoke(Method.java:511)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  12-24 14:43:10.915: E/AndroidRuntime(782):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>DB_Functions file</p>
  
  <pre><code>&lt;?php
  
  class DB_Functions {
  
      private $db;
  
      //put your code here
      // constructor
      function __construct() {
          require_once 'DB_Connect.php';
          // connecting to database
          $this-&gt;db = new DB_Connect();
          $this-&gt;db-&gt;connect();
      }
  
      // destructor
      function __destruct() {
  
      }
  
  
      /**
       * Random string which is sent by mail to reset password
       */
  
  public function random_string()
  {
      $character_set_array = array();
      $character_set_array[] = array('count' =&gt; 7, 'characters' =&gt; 'abcdefghijklmnopqrstuvwxyz');
      $character_set_array[] = array('count' =&gt; 1, 'characters' =&gt; '0123456789');
      $temp_array = array();
      foreach ($character_set_array as $character_set) {
          for ($i = 0; $i &lt; $character_set['count']; $i++) {
              $temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)];
          }
      }
      shuffle($temp_array);
      return implode('', $temp_array);
  }
  
  
  public function forgotPassword($forgotpassword, $newpassword, $salt){
      $result = mysql_query("UPDATE `users` SET `encrypted_password` = '$newpassword',`salt` = '$salt' 
                            WHERE `email` = '$forgotpassword'");
  
  if ($result) {
  
  return true;
  
  }
  else
  {
  return false;
  }
  
  }
  /**
       * Adding new user to mysql database
       * returns user details
       */
  
      public function storeUser($fname, $lname, $email, $uname, $password) {
          $uuid = uniqid('', true);
          $hash = $this-&gt;hashSSHA($password);
          $encrypted_password = $hash["encrypted"]; // encrypted password
          $salt = $hash["salt"]; // salt
          $result = mysql_query("INSERT INTO users(unique_id, firstname, lastname, email, username, encrypted_password, salt, created_at) VALUES('$uuid', '$fname', '$lname', '$email', '$uname', '$encrypted_password', '$salt', NOW())");
          // check for successful store
          if ($result) {
              // get user details 
              $uid = mysql_insert_id(); // last inserted id
              $result = mysql_query("SELECT * FROM users WHERE uid = $uid");
              // return user details
              return mysql_fetch_array($result);
          } else {
              return false;
          }
      }
  
      /**
       * Verifies user by email and password
       */
      public function getUserByEmailAndPassword($email, $password) {
          $result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
          // check for result 
          $no_of_rows = mysql_num_rows($result);
          if ($no_of_rows &gt; 0) {
              $result = mysql_fetch_array($result);
              $salt = $result['salt'];
              $encrypted_password = $result['encrypted_password'];
              $hash = $this-&gt;checkhashSSHA($salt, $password);
              // check for password equality
              if ($encrypted_password == $hash) {
                  // user authentication details are correct
                  return $result;
              }
          } else {
              // user not found
              return false;
          }
      }
  
  
   /**
       * Check user is existed or not
       */
      public function isUserExisted($email) {
          $result = mysql_query("SELECT email from users WHERE email = '$email'");
          $no_of_rows = mysql_num_rows($result);
          if ($no_of_rows &gt; 0) {
              // user existed 
              return true;
          } else {
              // user not existed
              return false;
          }
      }
  
      /**
       * Encrypting password
       * returns salt and encrypted password
       */
      public function hashSSHA($password) {
  
          $salt = sha1(rand());
          $salt = substr($salt, 0, 10);
          $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
          $hash = array("salt" =&gt; $salt, "encrypted" =&gt; $encrypted);
          return $hash;
      }
  
      /**
       * Decrypting password
       * returns hash string
       */
      public function checkhashSSHA($salt, $password) {
  
          $hash = base64_encode(sha1($password . $salt, true) . $salt);
  
          return $hash;
      }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>DB_Connect file</p>
  
  <pre><code>&lt;?php
  class DB_Connect {
  
      // constructor
      function __construct() {
  
      }
  
      // destructor
      function __destruct() {
          // $this-&gt;close();
      }
  
      // Connecting to database
      public function connect() {
          require_once 'include/config.php';
          // connecting to mysql
          $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
          // selecting database
          mysql_select_db(DB_DATABASE);
  
          // return database handler
          return $con;
      }
  
      // Closing database connection
      public function close() {
          mysql_close();
      }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>config file</p>
  
  <pre><code>&lt;?php
  
  /**
   * Database config variables
   */
  define("DB_HOST", "127.0.0.1");
  define("DB_USER", "root");
  define("DB_PASSWORD", "");
  define("DB_DATABASE", "learn2crack_login_api");
  ?&gt;
  </code></pre>
  
  <p>DatabaseHandler.java</p>
  
  <pre><code>package com.learn2crack.library;
  
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  import java.util.HashMap;
  
  public class DatabaseHandler extends SQLiteOpenHelper {
  
      // All Static variables
      // Database Version
      private static final int DATABASE_VERSION = 1;
  
      // Database Name
      private static final String DATABASE_NAME = "cloud_contacts";
  
      // Login table name
      private static final String TABLE_LOGIN = "login";
  
      // Login Table Columns names
      private static final String KEY_ID = "id";
      private static final String KEY_FIRSTNAME = "fname";
      private static final String KEY_LASTNAME = "lname";
      private static final String KEY_EMAIL = "email";
      private static final String KEY_USERNAME = "uname";
      private static final String KEY_UID = "uid";
      private static final String KEY_CREATED_AT = "created_at";
  
      public DatabaseHandler(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      // Creating Tables
      @Override
      public void onCreate(SQLiteDatabase db) {
          String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
                  + KEY_ID + " INTEGER PRIMARY KEY,"
                  + KEY_FIRSTNAME + " TEXT,"
                  + KEY_LASTNAME + " TEXT,"
                  + KEY_EMAIL + " TEXT UNIQUE,"
                  + KEY_USERNAME + " TEXT,"
                  + KEY_UID + " TEXT,"
                  + KEY_CREATED_AT + " TEXT" + ")";
          db.execSQL(CREATE_LOGIN_TABLE);
      }
  
      // Upgrading database
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // Drop older table if existed
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
  
          // Create tables again
          onCreate(db);
      }
  
      /**
       * Storing user details in database
       * */
      public void addUser(String fname, String lname, String email, String uname, String uid, String created_at) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(KEY_FIRSTNAME, fname); // FirstName
          values.put(KEY_LASTNAME, lname); // LastName
          values.put(KEY_EMAIL, email); // Email
          values.put(KEY_USERNAME, uname); // UserName
          values.put(KEY_UID, uid); // Email
          values.put(KEY_CREATED_AT, created_at); // Created At
  
          // Inserting Row
          db.insert(TABLE_LOGIN, null, values);
          db.close(); // Closing database connection
      }
  
  
      /**
       * Getting user data from database
       * */
      public HashMap&lt;String, String&gt; getUserDetails(){
          HashMap&lt;String,String&gt; user = new HashMap&lt;String,String&gt;();
          String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;
  
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor cursor = db.rawQuery(selectQuery, null);
          // Move to first row
          cursor.moveToFirst();
          if(cursor.getCount() &gt; 0){
              user.put("fname", cursor.getString(1));
              user.put("lname", cursor.getString(2));
              user.put("email", cursor.getString(3));
              user.put("uname", cursor.getString(4));
              user.put("uid", cursor.getString(5));
              user.put("created_at", cursor.getString(6));
          }
          cursor.close();
          db.close();
          // return user
          return user;
      }
  
  
  
  
  
  
      /**
       * Getting user login status
       * return true if rows are there in table
       * */
      public int getRowCount() {
          String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor cursor = db.rawQuery(countQuery, null);
          int rowCount = cursor.getCount();
          db.close();
          cursor.close();
  
          // return row count
          return rowCount;
      }
  
  
      /**
       * Re crate database
       * Delete all tables and create them again
       * */
      public void resetTables(){
          SQLiteDatabase db = this.getWritableDatabase();
          // Delete All Rows
          db.delete(TABLE_LOGIN, null, null);
          db.close();
      }
  
  }
  </code></pre>
  
  <p>JSONParser.java</p>
  
  <pre><code>package com.learn2crack.library;
  
  
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.UnsupportedEncodingException;
  import java.util.List;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.util.Log;
  
  public class JSONParser {
  
      static InputStream is = null;
      static JSONObject jObj = null;
      static String json = "";
  
      // constructor
      public JSONParser() {
  
      }
  
      public JSONObject getJSONFromUrl(String url, List&lt;NameValuePair&gt; params) {
  
          // Making HTTP request
          try {
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(url);
              httpPost.setEntity(new UrlEncodedFormEntity(params));
  
              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
  
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              json = sb.toString();
              Log.e("JSON", json);
          } catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
  
          // try parse the string to a JSON object
          try {
              jObj = new JSONObject(json);
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
  
          // return JSON String
          return jObj;
  
      }
  }
  </code></pre>
  
  <p>UserFunctions.java</p>
  
  <pre><code>package com.learn2crack.library;
  
  import java.util.ArrayList;
  import java.util.List;
  import org.apache.http.NameValuePair;
  import org.apache.http.message.BasicNameValuePair;
  import org.json.JSONObject;
  import android.content.Context;
  
  
  public class UserFunctions {
  
      private JSONParser jsonParser;
  
      //URL of the PHP API
      private static String loginURL = "http://10.0.2.2/learn2crack_login_api/";
      private static String registerURL = "http://10.0.2.2/learn2crack_login_api/";
      private static String forpassURL = "http://10.0.2.2/learn2crack_login_api/";
      private static String chgpassURL = "http://10.0.2.2/learn2crack_login_api/";
  
  
      private static String login_tag = "login";
      private static String register_tag = "register";
      private static String forpass_tag = "forpass";
      private static String chgpass_tag = "chgpass";
  
  
      // constructor
      public UserFunctions(){
          jsonParser = new JSONParser();
      }
  
      /**
       * Function to Login
       **/
  
      public JSONObject loginUser(String email, String password){
          // Building Parameters
          List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
          params.add(new BasicNameValuePair("tag", login_tag));
          params.add(new BasicNameValuePair("email", email));
          params.add(new BasicNameValuePair("password", password));
          JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
          return json;
      }
  
      /**
       * Function to change password
       **/
  
      public JSONObject chgPass(String newpas, String email){
          List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
          params.add(new BasicNameValuePair("tag", chgpass_tag));
  
          params.add(new BasicNameValuePair("newpas", newpas));
          params.add(new BasicNameValuePair("email", email));
          JSONObject json = jsonParser.getJSONFromUrl(chgpassURL, params);
          return json;
      }
  
  
  
  
  
      /**
       * Function to reset the password
       **/
  
      public JSONObject forPass(String forgotpassword){
          List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
          params.add(new BasicNameValuePair("tag", forpass_tag));
          params.add(new BasicNameValuePair("forgotpassword", forgotpassword));
          JSONObject json = jsonParser.getJSONFromUrl(forpassURL, params);
          return json;
      }
  
  
  
  
  
  
       /**
        * Function to  Register
        **/
      public JSONObject registerUser(String fname, String lname, String email, String uname, String password){
          // Building Parameters
          List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
          params.add(new BasicNameValuePair("tag", register_tag));
          params.add(new BasicNameValuePair("fname", fname));
          params.add(new BasicNameValuePair("lname", lname));
          params.add(new BasicNameValuePair("email", email));
          params.add(new BasicNameValuePair("uname", uname));
          params.add(new BasicNameValuePair("password", password));
          JSONObject json = jsonParser.getJSONFromUrl(registerURL,params);
          return json;
      }
  
  
      /**
       * Function to logout user
       * Resets the temporary data stored in SQLite Database
       * */
      public boolean logoutUser(Context context){
          DatabaseHandler db = new DatabaseHandler(context);
          db.resetTables();
          return true;
      }
  
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>: I turned off the error messages and changed mysql_* to mysqli. Now I get the following errors:</p>
  
  <pre><code>12-24 15:33:34.971: E/JSON(863): {"tag":"register","success":0,"error":1,"error_msg":"JSON Error occured in Registartion"}
  </code></pre>
  
  <p><strong>UPDATE</strong>: Problem solved. I restarted the application and now the registration and login is working fine. Thank you all for your support. </p>