使用android模拟器访问本地数据库

时间:2013-02-24 11:27:13

标签: php android json login registration

我正在与http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/合作。我在这里用JSON标记了几个问题。我现在不知道该怎么办。我想补充说我的本地主机是ar端口81.我刚刚在登录和注册网址中只更改了localhost:81 / android_login_api /和localhost:81 / android_login_api /。这是我的logcat错误 -

02-24 11:17:58.000: E/JSON(1364): <br />
02-24 11:17:58.000: E/JSON(1364): <b>Parse error</b>:  syntax error, unexpected $end in <b>C:\xampp\htdocs\android_login_api\include\DB_Connect.php</b> on line <b>46</b><br />
02-24 11:17:58.011: E/JSON Parser(1364): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
02-24 11:17:58.011: D/AndroidRuntime(1364): Shutting down VM
02-24 11:17:58.011: W/dalvikvm(1364): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-24 11:17:58.030: E/AndroidRuntime(1364): FATAL EXCEPTION: main
02-24 11:17:58.030: E/AndroidRuntime(1364): java.lang.NullPointerException
02-24 11:17:58.030: E/AndroidRuntime(1364):     at com.example.androidhive.RegisterActivity$1.onClick(RegisterActivity.java:64)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at android.view.View.performClick(View.java:4202)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at android.view.View$PerformClick.run(View.java:17340)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at android.os.Handler.handleCallback(Handler.java:725)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at android.os.Looper.loop(Looper.java:137)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at java.lang.reflect.Method.invoke(Method.java:511)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-24 11:17:58.030: E/AndroidRuntime(1364):     at dalvik.system.NativeStart.main(Native Method)

这是我的PHP代码 -

<?php
class DB_Connect {

    // constructor
    function __construct() {
    $sql = "CREATE TABLE IF NOT EXISTS users(
               uid int(11) primary key auto_increment,
               unique_id varchar(23) not null unique,
               name varchar(50) not null,
               email varchar(100) not null unique,
               encrypted_password varchar(80) not null,
               salt varchar(10) not null,
               created_at datetime,
               updated_at datetime null
            );
      mysql_query($sql);

    }

    // 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();
    }

}
?>

1 个答案:

答案 0 :(得分:0)

您的文件C:\xampp\htdocs\android_login_api\include\DB_Connect.php似乎包含语法错误。您的服务器返回HTML站点(包含此错误的信息)而不是JSON编码的结果字符串。

JSONParser需要类似"{somedata:true}"但得到<br />和其他HTML标签,然后抛出JSONException,最终返回null(这本身会导致{ {1}})

您可能需要查看this question