Domanda

Hello guys so i followed the androidhive for login/register android system and i added first name and lastname but for some reason when i click register my app crashes :(.Heres my code

DBConnect.php

 <?php

class DB_Connect {

    // constructor
    function __construct() {

    }

    // destructor
    function __destruct() {
        // $this->close();
    }

    // Connecting to database
    public function connect() {
        require_once '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();
    }

}

?>

ADDED : DBFunctions.php

<? db = new DB_Connect();
    $this->db->connect();
}

// destructor
function __destruct() {

}

/**
 * Storing new user
 * returns user details
 */
public function storeUser($name, $email, $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, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$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;
    }
}

/**
 * Get 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
 * @param 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
 * @param salt, password
 * returns hash string
 */
public function checkhashSSHA($salt, $password) {

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

    return $hash;
}

}

?>

I would really appreciate help cus im completely stuck :S

P.S: insta shutdown after sucsses register posted logcat down

logcat :

    02-18 21:53:14.245: D/ProgressBar(18032): setProgress = 0
02-18 21:53:14.245: D/ProgressBar(18032): setProgress = 0, fromUser = false
02-18 21:53:14.245: D/ProgressBar(18032): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
02-18 21:53:14.295: V/g f(18032):  f f
02-18 21:53:14.315: D/ProgressBar(18032): updateDrawableBounds: left = 0
02-18 21:53:14.315: D/ProgressBar(18032): updateDrawableBounds: top = 0
02-18 21:53:14.315: D/ProgressBar(18032): updateDrawableBounds: right = 64
02-18 21:53:14.315: D/ProgressBar(18032): updateDrawableBounds: bottom = 64
02-18 21:53:14.665: V/err(18032): {"tag":"register","success":1,"error":0,"uid":"5303acb7aa41f9.88773766","user":{"name":"yr","email":"g f ","created_at":"2014-02-18 20:55:51","updated_at":null}}
02-18 21:53:14.815: D/AndroidRuntime(18032): Shutting down VM
02-18 21:53:14.815: W/dalvikvm(18032): threadid=1: thread exiting with uncaught exception (group=0x40fbb930)
02-18 21:53:14.820: E/AndroidRuntime(18032): FATAL EXCEPTION: main
02-18 21:53:14.820: E/AndroidRuntime(18032): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.babycare/com.example.babycare.DashboardActivity}; have you declared this activity in your AndroidManifest.xml?
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1635)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1434)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.app.Activity.startActivityForResult(Activity.java:3430)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.app.Activity.startActivityForResult(Activity.java:3391)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.app.Activity.startActivity(Activity.java:3626)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.app.Activity.startActivity(Activity.java:3594)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at com.example.babycare.RegisterActivity.registerReport(RegisterActivity.java:87)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at com.example.babycare.RegisterTask.onPostExecute(RegisterTask.java:118)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at com.example.babycare.RegisterTask.onPostExecute(RegisterTask.java:1)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.os.AsyncTask.finish(AsyncTask.java:631)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.os.Looper.loop(Looper.java:137)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at android.app.ActivityThread.main(ActivityThread.java:5306)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at java.lang.reflect.Method.invokeNative(Native Method)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at java.lang.reflect.Method.invoke(Method.java:511)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-18 21:53:14.820: E/AndroidRuntime(18032):    at dalvik.system.NativeStart.main(Native Method)
02-18 21:53:56.860: I/Process(18032): Sending signal. PID: 18032 SIG: 9
02-18 21:53:57.435: D/dalvikvm(18161): GC_FOR_ALLOC freed 114K, 52% free 3998K/8304K, paused 11ms, total 11ms
02-18 21:53:57.485: I/dalvikvm-heap(18161): Grow heap (frag case) to 16.782MB for 9216016-byte allocation
02-18 21:53:57.500: E/dalvikvm(18161): adjustAdaptiveCoef max=8388608, min=2097152, ut=568
02-18 21:53:57.500: D/dalvikvm(18161): GC_FOR_ALLOC freed 2K, 25% free 12996K/17308K, paused 15ms, total 15ms
02-18 21:53:57.515: E/dalvikvm(18161): adjustAdaptiveCoef max=8388608, min=2097152, ut=368
02-18 21:53:57.515: D/dalvikvm(18161): GC_CONCURRENT freed <1K, 25% free 12996K/17308K, paused 2ms+1ms, total 13ms
02-18 21:53:57.640: D/dalvikvm(18161): GC_FOR_ALLOC freed 9309K, 55% free 11331K/24956K, paused 9ms, total 9ms
02-18 21:53:57.685: D/libEGL(18161): loaded /system/lib/egl/libEGL_mali.so
02-18 21:53:57.685: D/libEGL(18161): loaded /system/lib/egl/libGLESv1_CM_mali.so
02-18 21:53:57.690: D/libEGL(18161): loaded /system/lib/egl/libGLESv2_mali.so
02-18 21:53:57.690: E/(18161): Device driver API match
02-18 21:53:57.690: E/(18161): Device driver API version: 17
02-18 21:53:57.690: E/(18161): User space API version: 17 
02-18 21:53:57.690: E/(18161): mali: REVISION=Linux-r3p1-01rel1 BUILD_DATE=Fri May 10 18:36:49 KST 2013 
02-18 21:53:57.715: D/OpenGLRenderer(18161): Enabling debug mode 0
È stato utile?

Soluzione

Later edit:

For the localhost problem: I assume you are using an emulator since you are using localhost. If you are not using an emulator and you are testing on a device, your device will have to be rooted in order to make it understand the localhost (you will have to be able to access the hosts file).

For the emulator:

  1. If you are using eclipse as an IDE do the following: Open DDMS perspective (top right corner or Window->Open Perspective->DDMS). In the left panel choose your emulator. In the right panel choose the File Explorer tab and navigate to /system/etc. Select the hosts file and click on "Pull from device" (upper right corner, the disk symbol). This will save the hosts file to your file system. edit the host file and add this line: 10.0.2.2 localhost. Save the file and then go back to eclipse in the same DDMS perspective, select the hosts file and choose "Push a file to device" (upper right phone symbol) and select your hosts file.

  2. For other IDEs (I only work with eclipse so I do not know their tools) or as an alternative to Eclipse you can use the ADB command line tools. A good tutorial is this: http://imagekarthik.wordpress.com/2012/07/16/how-to-modify-android-emulator-hosts-file/

Please post if there are other problems.

Ok, so the exception from logcat should read this way:

"NetworkOnMainThreadException" means that you are trying to get information through the network from the UI thread (where your activity resides). Android forbids this because it is usually a time/process/resources consuming operation and would most probably block the ui thread and make the user think the application is non-responsive.

The best way to deal with this is to use an AsyncTask. A very good tutorial can be found here: http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

What you will have to do is change the UserFunction class to use the asyncTask for the register and login operations. Also, the onClick code from the register button from your main Activity should be refactored into the onPostExecute code of the AsyncTask for the register function in UserFunctions class.

Try to understand the concepts of asyncTask and android best practices for network operations. If you have further questions post them here.

Altri suggerimenti

As i suspected you have a NetworkOnMainThreadException. You cannot do network related operation on the ui thread.

You should use a Thread or a Asynctask for this purpose.

In UserFunctions you have

JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);

In JSONparser you have

HttpResponse httpResponse = httpClient.execute(httpPost); 
// not possible on the ui thread

Move the http request to Asynctask or Thread.

http://developer.android.com/reference/android/os/AsyncTask.html

Clear you logcat first:

$ adb logcat -c

Then run it again:

$ adb logcat

Now, launch your application

When the application crashes, copy the logcat output and paste it here.

You should do the network related tasks in the background thread using the doinbackground() of asyncTask

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top