dimanche 6 décembre 2015

Login and Registration in Android with PHP, MySQL and SQLite

Am trying to create a login/signup page for android app and store the data on the server. For now am using WAMP ( I plan to shift it to my server).

But the problem is,am not able to connect to the database only.

The code for the login page is :

    <?php
    require_once 'include/DB_Functions.php';
    $db = new DB_Functions();

    // json response array
    $response = array("error" => FALSE);

    if (isset($_POST['email']) && isset($_POST['password'])) {

        // receiving the post params
        $email = $_POST['email'];
        $password = $_POST['password'];

        // get the user by email and password
        $user = $db->getUserByEmailAndPassword($email, $password);

        if ($user != false) {
            // use is found
            $response["error"] = FALSE;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            echo json_encode($response);
        } else {
            // user is not found with the credentials
            $response["error"] = TRUE;
            $response["error_msg"] = "Login credentials are wrong. Please try again!";
            echo json_encode($response);
        }
    } else {
        // required post params is missing
        $response["error"] = TRUE;
        $response["error_msg"] = "Required parameters email or password is missing!";
        echo json_encode($response);
    }
    ?>

Php code for config.php and DB_Connect.php

<?php

/**
 * Database config variables
 */
define("DB_HOST", "http://192.168.43.17");
define("DB_USER", "root");
define("DB_PASSWORD", "YES");
define("DB_DATABASE", "android_api");
?>

<?php
class DB_Connect {
    private $conn;

    // Connecting to database
    public function connect() {
        require_once 'include/Config.php';

        // Connecting to mysql database
        $this->conn = new mysqli('http://192.168.43.17/', DB_USER, DB_PASSWORD, DB_DATABASE);

        // return database handler
        return $this->conn;

    }
}

?>

The logcat error msg is :

12-07 11:27:29.516 1926-6605/com.example.anubhaw.spirituplogin D/Volley﹕ [119] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://ift.tt/1N5J977 0x85e88f16 NORMAL 1> [lifetime=5562], [size=7082], [rc=200], [retryCount=1] 12-07 11:27:29.552 1926-1926/com.example.anubhaw.spirituplogin D/RegisterActivity﹕ Login Response: <br /> <font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'> <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\android_login_api\include\DB_Connect.php on line <i>10</i></th></tr> <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> <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> <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.1380</td><td bgcolor='#eeeeec' align='right'>246672</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\android_login_api\login.php' bgcolor='#eeeeec'>..\login.php<b>:</b>0</td></tr> <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.1880</td><td bgcolor='#eeeeec' align='right'>275480</td><td bgcolor='#eeeeec'>DB_Functions->__construct( )</td><td title='C:\wamp\www\android_login_api\login.php' bgcolor='#eeeeec'>..\login.php<b>:</b>3</td></tr> <tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.2260</td><td bgcolor='#eeeeec' align='right'>280048</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>12</td></tr> <tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.2260</td><td bgcolor='#eeeeec' align='right'>282184</td><td bgcolor='#eeeeec'><a href='http://ift.tt/VtBtFU' target='_new'>mysqli</a> ( )</td><td title='C:\wamp\www\android_login_api\include\DB_Connect.php' bgcolor='#eeeeec'>..\DB_Connect.php<b>:</b>10</td></tr> </table></font> <br /> <font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'> <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\android_login_api\include\DB_Connect.php on line <i>10</i></th></tr> <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> <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> <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.1380</td><td bgcolor='#eeeeec' align='right'>246672</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\android_login_api\login.php' bgcolor='#eeeeec'>..\login.php<b>:</b>0</td></tr> <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.1880</td><td bgcolor='#eeeeec' align='right'>275480</td><td bgcolor='#eeeeec'>DB_Functions->__construct( )</td><td title='C:\wamp\www\android_login_api\login.php' bgcolor='#eeeeec'>..\login.php<b>:</b>3</td></tr> <tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.2260</td><td bgcolor='#eeeeec' align='right'>280048</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>12</td></tr> <tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.2260</td><td bgcolor='#eeeeec' align='right'>282184</td><td bgcolor='#eeeeec'><a href='http://ift.tt/VtBtFU' target='_new'>mysqli</a> ( )</td><td title='C:\wamp\www\android_login_api\include\DB_Connect.php' bgcolor='#eeeeec'>..\DB_Connect.php<b>:</b>10</td>< 12-07 11:27:29.581 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 12-07 11:27:29.582 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111) 12-07 11:27:29.582 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:160) 12-07 11:27:29.582 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:173) 12-07 11:27:29.582 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at com.example.anubhaw.spirituplogin.LoginActivity$3.onResponse(LoginActivity.java:120) 12-07 11:27:29.582 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at com.example.anubhaw.spirituplogin.LoginActivity$3.onResponse(LoginActivity.java:112) 12-07 11:27:29.582 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 12-07 11:27:29.582 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 12-07 11:27:29.582 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 12-07 11:27:29.582 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739) 12-07 11:27:29.583 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95) 12-07 11:27:29.583 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at android.os.Looper.loop(Looper.java:135) 12-07 11:27:29.583 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5257) 12-07 11:27:29.583 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method) 12-07 11:27:29.583 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372) 12-07 11:27:29.583 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 12-07 11:27:29.593 1926-1926/com.example.anubhaw.spirituplogin W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 12-07 11:27:29.601 1926-1926/com.example.anubhaw.spirituplogin D/Volley﹕ [1] Request.finish: 5808 ms: [ ] http://ift.tt/1N5J977 0x85e88f16 NORMAL 1 12-07 11:27:29.654 1926-1957/com.example.anubhaw.spirituplogin W/EGL_emulation﹕ eglSurfaceAttrib not implemented 12-07 11:27:29.655 1926-1957/com.example.anubhaw.spirituplogin W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa50eeda0, error=EGL_SUCCESS

The code for LoginActivity is :

private void checkLogin(final String email, final String password) {
        // Tag used to cancel the request
        String tag_string_req = "req_login";

        pDialog.setMessage("Logging in ...");
        showDialog();

        StringRequest strReq = new StringRequest(Method.POST,
                AppConfig.URL_LOGIN, new Response.Listener<String>()//Line 112 {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Login Response: " + response.toString());
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);//line 120
                    boolean error = jObj.getBoolean("error");

                    // Check for error node in json
                    if (!error) {
                        // user successfully logged in
                        // Create login session
                        session.setLogin(true);

                        // Now store the user in SQLite
                        String uid = jObj.getString("uid");

                        JSONObject user = jObj.getJSONObject("user");
                        String name = user.getString("name");
                        String email = user.getString("email");
                        String created_at = user
                                .getString("created_at");

                        // Inserting row in users table
                        db.addUser(name, email, uid, created_at);

                        // Launch main activity
                        Intent intent = new Intent(LoginActivity.this,
                                MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        // Error in login. Get the error message
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }

            }
        }, new Response.ErrorListener() `

Can you please guide me,where am doing wrong.

Aucun commentaire:

Enregistrer un commentaire