0

I have made database and table at byethost server, generated apk of the code i saw from a tutorial(http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/) and then installed it on my device. but whenever i try to register, i only get an error message that is a part of UI. and when i try to click the login button in the login page, the app crashes.

code for layout:

<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>

the code for the java file :

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

edit : logcat when i press login button

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)
Jonas452
  • 420
  • 6
  • 19
Akash
  • 220
  • 3
  • 12
  • tutorial i referred : http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ – Akash Jul 30 '14 at 15:24
  • Show us the error log. – Jonas452 Jul 30 '14 at 15:25
  • i tried it on a device after building it, does it also give an error log? or should i run it in an emulator? – Akash Jul 30 '14 at 15:26
  • Are you doing USB emulation from Eclipse? If yes, in Eclipse go in Window > Show View > Others > LogCat. When something happens while you app is running it'll be show in the LogCat. – Jonas452 Jul 30 '14 at 15:28
  • nope i generate an apk, copy, install and run it on device – Akash Jul 30 '14 at 15:29
  • i can run it on a VM, but i am unsure if it can connect to the database froma VM. can it connect? – Akash Jul 30 '14 at 15:29
  • Do the USB Emulation from Eclipse, it's easer and better them generating the apk, so you can use the LogCat. And I don't think that'll connect. – Jonas452 Jul 30 '14 at 15:32
  • Here is a tutorial of USB Emulation: http://developer.android.com/tools/building/building-eclipse.html – Jonas452 Jul 30 '14 at 15:33
  • Use the USB Emulation and the LogCat and make the error happens so he can return you a exception, and then we can help you. – Jonas452 Jul 30 '14 at 15:33

1 Answers1

0

First, you should learn more about AsyncTask. Here is a tutorial: http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

Is not good to do that kind of work at the UI Thread.

And your JSON code has to improve. Here is other tutorial: http://www.vogella.com/tutorials/AndroidJSON/article.html

Try to create a class that'll make the connections for your and get the JSONObject from the URL that you pass. You have to deal with the exception.

Jonas452
  • 420
  • 6
  • 19