Android应用程序登录和注册错误

Posted

技术标签:

【中文标题】Android应用程序登录和注册错误【英文标题】:Android application login and registration error 【发布时间】:2015-02-22 16:20:33 【问题描述】:

我一直在关注使用 phpmysql 开发安卓登录和注册系统的教程。注册用户时,我的本地数据库 (xampp) 中添加了一条记录,但应用程序崩溃了。我已经查看了有关此问题的类似问题,但我仍然感到困惑并且找不到解决方案。

日志猫:

12-24 14:43:10.875: E/JSON(782): <br />
12-24 14:43:10.875: E/JSON(782): <b>Deprecated</b>:  mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in <b>G:\xampp\htdocs\learn2crack_login_api\include\DB_Connect.php</b> on line <b>18</b><br />
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 <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)

DB_Functions 文件

<?php

class DB_Functions 

    private $db;

    //put your code here
    // constructor
    function __construct() 
        require_once 'DB_Connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->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' => 7, 'characters' => 'abcdefghijklmnopqrstuvwxyz');
    $character_set_array[] = array('count' => 1, 'characters' => '0123456789');
    $temp_array = array();
    foreach ($character_set_array as $character_set) 
        for ($i = 0; $i < $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->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 > 0) 
            $result = mysql_fetch_array($result);
            $salt = $result['salt'];
            $encrypted_password = $result['encrypted_password'];
            $hash = $this->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 > 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" => $salt, "encrypted" => $encrypted);
        return $hash;
    

    /**
     * Decrypting password
     * returns hash string
     */
    public function checkhashSSHA($salt, $password) 

        $hash = base64_encode(sha1($password . $salt, true) . $salt);

        return $hash;
    



?>

DB_Connect 文件

<?php
class DB_Connect 

    // constructor
    function __construct() 

    

    // destructor
    function __destruct() 
        // $this->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();
    



?>

配置文件

<?php

/**
 * Database config variables
 */
define("DB_HOST", "127.0.0.1");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "learn2crack_login_api");
?>

DatabaseHandler.java

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<String, String> getUserDetails()
        HashMap<String,String> user = new HashMap<String,String>();
        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() > 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();
    


JSONParser.java

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<NameValuePair> 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;

    

UserFunctions.java

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<NameValuePair> params = new ArrayList<NameValuePair>();
        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<NameValuePair> params = new ArrayList<NameValuePair>();
        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<NameValuePair> params = new ArrayList<NameValuePair>();
        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<NameValuePair> params = new ArrayList<NameValuePair>();
        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;
    


更新:我关闭了错误信息并将 mysql_* 更改为 mysqli。现在我收到以下错误:

12-24 15:33:34.971: E/JSON(863): "tag":"register","success":0,"error":1,"error_msg":"JSON Error occured in Registartion"

更新:问题已解决。我重新启动了应用程序,现在注册和登录工作正常。谢谢大家的支持。

【问题讨论】:

【参考方案1】:

检查 json 的前几行:

<b>Deprecated</b>:  mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in <b>G:\xampp\htdocs\learn2crack_login_api\include\DB_Connect.php</b> on line <b>18</b><br /

由于此错误消息,您无法解析 json。 关闭错误消息以便快速修复。

但对于真正的解决方案,不要使用 mysql_* 函数。正如消息所说,它们已被弃用。请改用 mysqli_* 或 PDO。它们很简单,并且通过准备好的语句,它们是安全的。

【讨论】:

好主意,提一下准备好的语句,他当前的代码看起来对 SQL 注入开放。 它不仅看起来,而且很容易受到攻击。 :) 嗨,Gerifield 我关闭了错误消息,现在我的屏幕卡在“正在联系服务器,正在注册”。我收到以下错误:12-24 15:33:34.971: E/JSON(863): "tag":"register","success":0,"error":1,"error_msg":"JSON 错误发生在注册” json 本身现在是正确的。这是另一个错误和问题。连接正常,可能检查解析器。【参考方案2】:

问题似乎是来自 PHP 代码的响应不是您的应用程序所期望的 JSON 字符串。 PHP 页面提供的是对mysql_connect 的弃用警告。

As per the PHP docs、mysql_connect 已弃用,取而代之的是 mysqli_connect

或者,您可以更改error level that PHP uses not to display the deprecation message。

我最初认为您可以将 JSON 解码调用包装在 try/catch 中,但是在进一步检查您的代码之后,看起来您已经涵盖了这一点。相反,我意识到它可能是您的代码的其他部分未处理 null 值,如您提供的日志文件中所示稍低的值。确保您有适当的代码来检查可能的 null 值,以防止您的应用程序崩溃,在生产/实时环境中很容易发生这种情况。

【讨论】:

【参考方案3】:

您从服务器接收的字符串不是有效的JSON 字符串。

参考下面的字符串:

<b>Deprecated</b>:  mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in <b>G:\xampp\htdocs\learn2crack_login_api\include\DB_Connect.php</b> on line <b>18</b><br />

这意味着,您应该切换到 mysqli 或 PDO,因为 mysql 扩展已被贬值。

替代解决方案: 关闭服务器上的所有 PHP 错误和警告。

【讨论】:

您不需要关闭所有错误和警告,您只需关闭弃用消息即可。 是的,但是在开发服务器上,而不是在实时服务器上。而是在每日日志文件中记录错误和警告。 您好 MTahir,我关闭了错误消息,现在我的屏幕卡在“正在联系服务器,正在注册”。我收到以下错误:12-24 15:33:34.971: E/JSON(863): "tag":"register","success":0,"error":1,"error_msg":"JSON 错误发生在注册”

以上是关于Android应用程序登录和注册错误的主要内容,如果未能解决你的问题,请参考以下文章

Android studio离线登录注册

Android实现qq登录注册和好友列表界面

android项目——登录界面

android如何实现 登陆以及注册

android studio登录注册

Android 和 iOS 上的 Facebook 用户 ID