Android应用程序在登录和注册时崩溃

时间:2014-05-25 15:21:01

标签: java php android mysql database

我的申请需要帮助,它适用于学校项目。在该应用程序中,我想进行登录和注册,但每当我按下登录并注册新帐户按钮时,它都会使整个应用程序崩溃。我使用wamp 2.5作为服务器。

loginactivity.java

package com.yang.hotelreceptiondemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yang.hotelreceptiondemo.library.UserFunctions;

public class LoginActivity extends Activity {
    public Button btnLogin;
    Button btnLinkToRegister;
    EditText inputEmail;
    EditText inputPassword;
    public TextView loginErrorMsg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        // Importing all assets like buttons, text fields
        inputEmail = (EditText) findViewById(R.id.loginEmail);
        inputPassword = (EditText) findViewById(R.id.loginPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
        loginErrorMsg = (TextView) findViewById(R.id.login_error);

        // Login button Click Event
        btnLogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                String email = inputEmail.getText().toString();
                String password = inputPassword.getText().toString();
                UserFunctions userFunction = new UserFunctions();
                Log.d("Button", "Login");
                new LoginAsyncTask(LoginActivity.this, userFunction).execute(
                        email, password);
                btnLogin.setEnabled(false);
            }
        });

        // Link to Register Screen
        btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(),
                        RegisterActivity.class);
                startActivity(i);
                finish();
            }
        });
    }
}

LoginAsyncTask.java

package com.yang.hotelreceptiondemo;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;

import com.yang.hotelreceptiondemo.library.DatabaseHandler;
import com.yang.hotelreceptiondemo.library.UserFunctions;

public class LoginAsyncTask extends AsyncTask<String, Void, JSONObject> {

    private UserFunctions userFunction;
    private LoginActivity activity;

    // JSON Response node names
    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR = "error";
    private static String KEY_ERROR_MSG = "error_msg";
    private static String KEY_UID = "uid";
    private static String KEY_NAME = "name";
    private static String KEY_EMAIL = "email";
    private static String KEY_CREATED_AT = "created_at";

    public LoginAsyncTask(LoginActivity activity, UserFunctions userFunction) {
        this.activity = activity;
        this.userFunction = userFunction;
    }

    @Override
    protected JSONObject doInBackground(String... datas) {
        return userFunction.loginUser(datas[0], datas[1]);
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        activity.btnLogin.setEnabled(true);
        // check for login response
        try {
            if (json.getString(KEY_SUCCESS) != null) {
                activity.loginErrorMsg.setText("");
                String res = json.getString(KEY_SUCCESS);
                if (Integer.parseInt(res) == 1) {
                    // user successfully logged in
                    // Store user details in SQLite Database
                    DatabaseHandler db = new DatabaseHandler(
                            activity.getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");

                    // Clear all previous data in database
                    userFunction.logoutUser(activity.getApplicationContext());
                    db.addUser(json_user.getString(KEY_NAME),
                            json_user.getString(KEY_EMAIL),
                            json.getString(KEY_UID),
                            json_user.getString(KEY_CREATED_AT));

                    // Launch Dashboard Screen
                    Intent dashboard = new Intent(
                            activity.getApplicationContext(),
                            DashboardActivity.class);

                    // Close all views before launching Dashboard
                    dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    activity.startActivity(dashboard);

                    // Close Login Screen
                    activity.finish();
                } else {
                    // Error in login
                    activity.loginErrorMsg
                            .setText("Incorrect username/password");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

log.txt的

05-25 23:19:48.600: E/JSON(3414): <br />
05-25 23:19:48.600: E/JSON(3414): <font size='1'><table class='xdebug-error xe-deprecated' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
05-25 23:19:48.600: E/JSON(3414): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\android_login_api\include\DB_Connect.php on line <i>18</i></th></tr>
05-25 23:19:48.600: E/JSON(3414): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
05-25 23:19:48.600: E/JSON(3414): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
05-25 23:19:48.600: E/JSON(3414): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>252360</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\android_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
05-25 23:19:48.600: E/JSON(3414): <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0013</td><td bgcolor='#eeeeec' align='right'>276160</td><td bgcolor='#eeeeec'>DB_Functions->__construct(  )</td><td title='C:\wamp\www\android_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>19</td></tr>
05-25 23:19:48.600: E/JSON(3414): <tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0019</td><td bgcolor='#eeeeec' align='right'>282296</td><td bgcolor='#eeeeec'>DB_Connect->connect(  )</td><td title='C:\wamp\www\android_login_api\include\DB_Functions.php' bgcolor='#eeeeec'>..\DB_Functions.php<b>:</b>13</td></tr>
05-25 23:19:48.600: E/JSON(3414): <tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.0027</td><td bgcolor='#eeeeec' align='right'>283136</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-connect' target='_new'>mysql_connect</a>
05-25 23:19:48.600: E/JSON(3414): (  )</td><td title='C:\wamp\www\android_login_api\include\DB_Connect.php' bgcolor='#eeeeec'>..\DB_Connect.php<b>:</b>18</td></tr>
05-25 23:19:48.600: E/JSON(3414): </table></font>
05-25 23:19:48.600: E/JSON(3414): {"tag":"login","success":1,"error":0,"uid":"5381f93b6fabb8.91945568","user":{"name":"testing","email":"testing@hotmail.com","created_at":"2014-05-25 22:07:55","updated_at":null}}
05-25 23:19:48.610: E/JSON Parser(3414): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
05-25 23:19:48.610: D/AndroidRuntime(3414): Shutting down VM
05-25 23:19:48.610: W/dalvikvm(3414): threadid=1: thread exiting with uncaught exception (group=0xb2c07180)
05-25 23:19:48.610: I/Process(3414): Sending signal. PID: 3414 SIG: 9
05-25 23:19:48.610: D/AndroidRuntime(3414): procName from cmdline: com.yang.hotelreceptiondemo
05-25 23:19:48.610: E/AndroidRuntime(3414): in writeCrashedAppName, pkgName :com.yang.hotelreceptiondemo
05-25 23:19:48.610: D/AndroidRuntime(3414): file written successfully with content: com.yang.hotelreceptiondemo StringBuffer : ;com.yang.hotelreceptiondemo
05-25 23:19:48.610: E/AndroidRuntime(3414): FATAL EXCEPTION: main
05-25 23:19:48.610: E/AndroidRuntime(3414): java.lang.NullPointerException
05-25 23:19:48.610: E/AndroidRuntime(3414):     at com.yang.hotelreceptiondemo.LoginAsyncTask.onPostExecute(LoginAsyncTask.java:41)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at com.yang.hotelreceptiondemo.LoginAsyncTask.onPostExecute(LoginAsyncTask.java:1)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at android.os.AsyncTask.finish(AsyncTask.java:602)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at android.os.Looper.loop(Looper.java:137)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at java.lang.reflect.Method.invoke(Method.java:511)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
05-25 23:19:48.610: E/AndroidRuntime(3414):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

看起来您的json结果已损坏,因为有关已弃用的mysql_connect的php警告。 在php.ini中,您可以通过排除已弃用的警告来隐藏这些警告:

error_reporting = E_ALL ^ E_DEPRECATED

重新启动您的网络服务器,然后重试。