Android上使用firebase检索数据错误

时间:2017-07-07 03:59:56

标签: android firebase firebase-realtime-database

我的Android应用与Firebase存在一些问题。

问题是我使用Firebase在Android Studio上为我的应用创建了一个登录功能,但我必须单击“登录”按钮两次。您只是在第一次单击按钮时不会检索数据。谢谢。

这是java代码。

 package com.jax.android.jaxsqrcodeattendancesystemsecond;

    import android.content.Intent;
    import android.os.AsyncTask;
    import android.support.v7.app.AppCompatActivity;
    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 android.widget.Toast;

    import com.google.firebase.database.ChildEventListener;
    import com.google.firebase.database.DataSnapshot;
    import com.google.firebase.database.DatabaseError;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.Query;
    import com.google.gson.Gson;

    public class LoginActivity extends AppCompatActivity {
        private EditText username;
        private EditText password;
        private Button buttonLogin;
        private TextView registerHere;

        boolean validated = false;
        String TAG = "TAG: ";
        public User user = new User();
        DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference("user");

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

            username = (EditText)findViewById(R.id.etUsername);
            password = (EditText)findViewById(R.id.etPassword);
            buttonLogin = (Button)findViewById(R.id.buttonLogin);
            registerHere = (TextView)findViewById(R.id.tvRegisterHere);



            //Login Button
            buttonLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   AsyncLoginTask task  = new AsyncLoginTask();
                    task.execute();
                    System.out.println("There are " + "User name : " +" " +  user.getUserName() + " "+ "user password " + " :"
                            + user.getPassword() );


                }
            });

            registerHere.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                    startActivity(intent);
                }
            });
        }

        private class AsyncLoginTask extends AsyncTask<String, Void, Void>{

            @Override
            protected Void doInBackground(String... params) {
                Log.i(TAG,"doInBackground");
                if(validated){
                    // open next activity
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);

                }
                return null;
            }

            @Override
            protected void onPreExecute() {
                String mUsername = username.getText().toString();
                String mPassword = password.getText().toString();
                Log.i(TAG, "onPreExecute");
                Query query =  mDatabaseReference.orderByChild("userName").equalTo(mUsername);
                query.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        user  = dataSnapshot.getValue(User.class);
                    }

                    @Override
                    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onChildRemoved(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });


                if (mUsername != "" && mUsername.equals(user.getUserName())){

                    if (password.getText().length()!= 0 && mPassword.equals(user.getPassword())){
                        validated = true;

                    }else{
                        Toast toast = Toast.makeText(getApplicationContext(), "User name and password don't match", Toast.LENGTH_LONG);
                        toast.show();
                        validated = false;
                    }

                }else{
                    Toast toast = Toast.makeText(getApplicationContext(), "please enter User name!", Toast.LENGTH_LONG);
                    toast.show();
                    validated = false;
                }

            }

            @Override
            protected void onPostExecute(Void aVoid) {
                if (validated){

                    Toast toast = Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG);
                    toast.show();
                }
                else{
                    Toast toast = Toast.makeText(getApplicationContext(), "Error Occured!", Toast.LENGTH_LONG);
                    toast.show();
                }

               /* if (user.getName() != null && user != null) {
                    Gson gson = new Gson();
                    String myJson = gson.toJson(user);
                    Intent intent = new Intent( getApplicationContext() ,MainActivity.class);
                    intent.putExtra("USER_INFO", myJson);
                    startActivity(intent);

                }*/

            }
        }

    }

布局XML

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jax.android.jaxsqrcodeattendancesystemsecond.LoginActivity">


    <EditText
        android:id="@+id/etUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Username"
        />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/buttonLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:text="Login" />

    <TextView
        android:id="@+id/tvRegisterHere"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register Here"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:layout_gravity="center"
        />
</LinearLayout>

LogCat

07-07 11:43:11.722 2888-2888/? I/art: Not late-enabling -Xcheck:jni (already on)
07-07 11:43:11.723 2888-2888/? W/art: Unexpected CPU variant for X86 using defaults: x86
07-07 11:43:11.745 2888-2896/? E/art: Failed sending reply to debugger: Broken pipe
07-07 11:43:11.745 2888-2896/? I/art: Debugger is no longer active
07-07 11:43:11.745 2888-2896/? I/art: Starting a blocking GC Instrumentation
07-07 11:43:11.840 2888-2888/? W/System: ClassLoader referenced unknown path: /data/app/com.jax.android.jaxsqrcodeattendancesystemsecond-1/lib/x86
07-07 11:43:11.850 2888-2888/? D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
07-07 11:43:11.856 2888-2888/? D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
07-07 11:43:11.865 2888-2888/? I/FA: App measurement is starting up, version: 11011
07-07 11:43:11.865 2888-2888/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
07-07 11:43:11.877 2888-2888/? V/FA: Collection enabled
07-07 11:43:11.878 2888-2888/? V/FA: App package, google app id: com.jax.android.jaxsqrcodeattendancesystemsecond, 1:1070300143559:android:089e2e22ef09d40c
07-07 11:43:11.878 2888-2888/? I/FA: To enable faster debug mode event logging run:
                                       adb shell setprop debug.firebase.analytics.app com.jax.android.jaxsqrcodeattendancesystemsecond
07-07 11:43:11.878 2888-2888/? D/FA: Debug-level message logging enabled
07-07 11:43:11.881 2888-2888/? V/FA: Cancelling job. JobID: 803327265
07-07 11:43:11.883 2888-2888/? V/FA: Registered activity lifecycle callback
07-07 11:43:11.883 2888-2888/? I/FirebaseInitProvider: FirebaseApp initialization successful
07-07 11:43:11.900 2888-2918/? V/FA: Using measurement service
07-07 11:43:11.901 2888-2918/? V/FA: Connecting to remote service
07-07 11:43:11.903 2888-2888/? W/GooglePlayServicesUtil: Google Play services out of date.  Requires 11011000 but found 10298470
07-07 11:43:11.903 2888-2888/? I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:0
07-07 11:43:11.903 2888-2888/? I/DynamiteModule: Selected local version of com.google.android.gms.firebase_database
07-07 11:43:11.903 2888-2918/? W/GooglePlayServicesUtil: Google Play services out of date.  Requires 11011000 but found 10298470
07-07 11:43:11.909 2888-2918/? V/FA: Using measurement service
07-07 11:43:11.910 2888-2918/? V/FA: Connection attempt already in progress
07-07 11:43:11.912 2888-2888/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-07 11:43:11.914 2888-2888/? V/FA: onActivityCreated
07-07 11:43:11.940 2888-2921/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
07-07 11:43:11.970 2888-2918/? V/FA: Using measurement service
07-07 11:43:11.970 2888-2918/? V/FA: Connection attempt already in progress
07-07 11:43:11.971 2888-2918/? V/FA: Activity resumed, time: 16635
07-07 11:43:11.976 2888-2918/? I/FA: Tag Manager is not found and thus will not be used
07-07 11:43:11.977 2888-2918/? D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=LoginActivity, firebase_screen_id(_si)=1414208018607190867}]
07-07 11:43:11.997 2888-2888/? W/FA: Service connection failed: ConnectionResult{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null, message=null}
07-07 11:43:12.014 2888-2918/? V/FA: Using measurement service
07-07 11:43:12.014 2888-2918/? V/FA: Connecting to remote service
07-07 11:43:12.019 2888-2918/? W/GooglePlayServicesUtil: Google Play services out of date.  Requires 11011000 but found 10298470
07-07 11:43:12.020 2888-2918/? V/FA: Processing queued up service tasks: 4
07-07 11:43:12.020 2888-2918/? E/FA: Discarding data. Failed to send app launch
07-07 11:43:12.020 2888-2918/? E/FA: Failed to get app instance id
07-07 11:43:12.020 2888-2918/? E/FA: Failed to send current screen to service
07-07 11:43:12.020 2888-2918/? E/FA: Discarding data. Failed to send event to service
07-07 11:43:12.068 2888-2932/? I/OpenGLRenderer: Initialized EGL, version 1.4
07-07 11:43:12.068 2888-2932/? D/OpenGLRenderer: Swap behavior 1
07-07 11:43:12.068 2888-2932/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
07-07 11:43:12.068 2888-2932/? D/OpenGLRenderer: Swap behavior 0
07-07 11:43:12.406 2888-2888/? W/FA: Service connection failed: ConnectionResult{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null, message=null}
07-07 11:43:12.407 2888-2918/? V/FA: Processing queued up service tasks: 0
07-07 11:43:33.469 2888-2888/com.jax.android.jaxsqrcodeattendancesystemsecond W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
07-07 11:43:39.465 2888-2888/com.jax.android.jaxsqrcodeattendancesystemsecond I/TAG:: onPreExecute
07-07 11:43:39.471 2888-2888/com.jax.android.jaxsqrcodeattendancesystemsecond I/System.out: There are User name :  null user password  :null
07-07 11:43:39.471 2888-2941/com.jax.android.jaxsqrcodeattendancesystemsecond I/TAG:: doInBackground
07-07 11:43:39.675 2888-2919/com.jax.android.jaxsqrcodeattendancesystemsecond W/PersistentConnection: pc_0 - Using an unspecified index. Consider adding '".indexOn": "userName"' at user to your security and Firebase Database rules for better performance
07-07 11:43:44.664 2888-2888/com.jax.android.jaxsqrcodeattendancesystemsecond I/TAG:: onPreExecute
07-07 11:43:44.665 2888-2888/com.jax.android.jaxsqrcodeattendancesystemsecond I/System.out: There are User name :  test01 user password  :123
07-07 11:43:44.667 2888-2942/com.jax.android.jaxsqrcodeattendancesystemsecond I/TAG:: doInBackground
07-07 11:43:44.679 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Recording user engagement, ms: 32709
07-07 11:43:44.680 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Using measurement service
07-07 11:43:44.680 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Connecting to remote service
07-07 11:43:44.682 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond W/GooglePlayServicesUtil: Google Play services out of date.  Requires 11011000 but found 10298470
07-07 11:43:44.682 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Activity paused, time: 49340
07-07 11:43:44.683 2888-2888/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: onActivityCreated
07-07 11:43:44.688 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=32709, firebase_screen_class(_sc)=LoginActivity, firebase_screen_id(_si)=1414208018607190867}]
07-07 11:43:44.705 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Using measurement service
07-07 11:43:44.705 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Connection attempt already in progress
07-07 11:43:44.728 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Using measurement service
07-07 11:43:44.728 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Connection attempt already in progress
07-07 11:43:44.728 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Activity resumed, time: 49394
07-07 11:43:44.730 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=LoginActivity, firebase_previous_id(_pi)=1414208018607190867, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=1414208018607190868}]
07-07 11:43:44.739 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Using measurement service
07-07 11:43:44.739 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Connection attempt already in progress
07-07 11:43:45.059 2888-2888/com.jax.android.jaxsqrcodeattendancesystemsecond W/FA: Service connection failed: ConnectionResult{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null, message=null}
07-07 11:43:45.061 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond V/FA: Processing queued up service tasks: 4
07-07 11:43:45.061 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond E/FA: Failed to send current screen to service
07-07 11:43:45.061 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond E/FA: Discarding data. Failed to send event to service
07-07 11:43:45.061 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond E/FA: Failed to send current screen to service
07-07 11:43:45.061 2888-3455/com.jax.android.jaxsqrcodeattendancesystemsecond E/FA: Discarding data. Failed to send event to service
07-07 11:43:45.198 2888-2888/com.jax.android.jaxsqrcodeattendancesystemsecond W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
07-07 11:43:45.224 2888-2932/com.jax.android.jaxsqrcodeattendancesystemsecond D/OpenGLRenderer: endAllActiveAnimators on 0x9431e800 (RippleDrawable) with handle 0x943526d0

Logcat错误。

Logcat Error

请参阅,当我第一次单击登录按钮时,它没有任何数据。

See, When I first time click the login button, it doesn't have any data

1 个答案:

答案 0 :(得分:0)

我使用Firebase authentication重新命令你。您不需要编写自己的算法代码,也不需要在将来维护它。您也可以使用Google signin

希望它有所帮助。

相关问题