从android连接到sql db时出错

时间:2014-07-30 15:16:19

标签: php android sql json sqlite

我在byethost服务器上创建了数据库和表,生成了我从教程(http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/)看到的代码的apk,然后将其安装在我的设备上。但每当我尝试注册时,我只会收到一条错误消息,这是UI的一部分。当我尝试单击登录页面中的登录按钮时,应用程序崩溃。

布局代码:          

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dip" >
    <!--  View Title Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="LOGIN"
        android:textSize="25dip"
        android:textStyle="bold" />
    <!--  Email Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Email" />
    <!--  Email TextField -->
    <EditText
        android:id="@+id/loginEmail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <!--  Password Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dip"
        android:text="Password" />
    <!--  Password TextField -->
    <EditText
        android:id="@+id/loginPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:password="true" />

    <!--  Error message -->
    <TextView android:id="@+id/login_error"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#e30000"
                android:padding="10dip"
                android:textStyle="bold"/>
    <!--  Login Button -->        
    <Button
        android:id="@+id/btnLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:text="Login" />
     <!--  Link to Registration Screen -->
     <Button
        android:id="@+id/btnLinkToRegisterScreen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dip"
        android:background="@null"
        android:text="I don&apos;t have account. Register Me!"
        android:textColor="#21dbd4"
        android:textStyle="bold" />
     </LinearLayout>

    </ScrollView>

java文件的代码:

import java.util.HashMap;

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

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.example.androidhive.library.DatabaseHandler;
 import com.example.androidhive.library.UserFunctions;

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

// 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";

@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");
            JSONObject json = userFunction.loginUser(email, password);

            // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    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(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        // Clear all previous data in database
                        userFunction.logoutUser(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(getApplicationContext(), DashboardActivity.class);

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

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

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

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

编辑:按下登录按钮时的logcat

07-30 15:32:15.488: I/Choreographer(867): Skipped 81 frames!  The application may be doing too much work on its main thread.
07-30 15:32:31.938: D/Button(867): Login
07-30 15:32:35.650: E/JSON(867): No database selected
07-30 15:32:35.670: E/JSON Parser(867): Error parsing data org.json.JSONException: Value No of type java.lang.String cannot be converted to JSONObject
07-30 15:32:35.679: D/AndroidRuntime(867): Shutting down VM
07-30 15:32:35.690: W/dalvikvm(867): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-30 15:32:35.753: E/AndroidRuntime(867): FATAL EXCEPTION: main
07-30 15:32:35.753: E/AndroidRuntime(867): java.lang.NullPointerException
07-30 15:32:35.753: E/AndroidRuntime(867):  at com.example.androidhive.LoginActivity$1.onClick(LoginActivity.java:65)
07-30 15:32:35.753: E/AndroidRuntime(867):  at android.view.View.performClick(View.java:4204)
07-30 15:32:35.753: E/AndroidRuntime(867):  at android.view.View$PerformClick.run(View.java:17355)
07-30 15:32:35.753: E/AndroidRuntime(867):  at android.os.Handler.handleCallback(Handler.java:725)
07-30 15:32:35.753: E/AndroidRuntime(867):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 15:32:35.753: E/AndroidRuntime(867):  at android.os.Looper.loop(Looper.java:137)
07-30 15:32:35.753: E/AndroidRuntime(867):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-30 15:32:35.753: E/AndroidRuntime(867):  at java.lang.reflect.Method.invokeNative(Native Method)
07-30 15:32:35.753: E/AndroidRuntime(867):  at java.lang.reflect.Method.invoke(Method.java:511)
07-30 15:32:35.753: E/AndroidRuntime(867):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 15:32:35.753: E/AndroidRuntime(867):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 15:32:35.753: E/AndroidRuntime(867):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

首先,您应该了解有关AsyncTask的更多信息。这是一个教程:http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

在UI Thread上做这种工作并不好。

您的JSON代码必须改进。 这是其他教程: http://www.vogella.com/tutorials/AndroidJSON/article.html

尝试创建一个类,它将为您创建连接,并从您传递的URL获取JSONObject。你必须处理这个例外。