Firebase DatabaseReference允许读取数据但不允许插入数据

时间:2017-07-01 01:34:27

标签: android firebase-realtime-database

我使用firebase在Android中编写应用程序。在这个类中,方法getTimestampFromServer()工作正常。在侦听器的onDataChange事件中,它调用方法updateCertificadoKey()。此方法在数据库引用中插入一个值,但没有任何反应。听众不起作用。

我尝试使用addOnCompleteListener()addListenerForSingleValueEvent()addValueEventListener(),但没有任何反应。此引用允许从数据库读取数据,但不允许插入或更新数据。

我修改了网址并且没问题。数据库没有限制(read = true,write = true)。

这是我的gradle配置

compile files('libs/itextg-5.5.8.jar')
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:cardview-v7:25.2.0'
compile 'com.google.firebase:firebase-core:9.2.0'
compile 'com.google.firebase:firebase-database:9.2.0'
compile 'com.google.firebase:firebase-crash:9.2.0'
compile 'com.google.firebase:firebase-auth:9.2.0'
compile 'com.google.firebase:firebase-config:9.2.0'
compile 'com.google.firebase:firebase-messaging:9.2.0'
compile 'com.firebase:firebase-client-android:2.4.0'
compile 'com.firebaseui:firebase-ui:0.4.3'
compile 'com.google.dagger:dagger:2.2'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.+'
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
apt 'com.squareup:javapoet:1.7.0'
apt 'com.google.dagger:dagger-compiler:2.2'
}

apply plugin: 'com.google.gms.google-services' //para firebase client

这是代码片段:

private void getTimestampFromServer(){
    DatabaseReference refOffset = FirebaseDatabase.getInstance().getReference(remoteConfig.getUrlTimeOffset());
    refOffset.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String aux = String.valueOf(dataSnapshot.getValue());
            long offset = Long.parseLong(aux);
            long timestamp = GregorianCalendar.getInstance().getTimeInMillis() + offset;
            updateCertificadoKey(timestamp);
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            showError(activity.getString(R.string.certificado_err_consec) + "\n" +
                    databaseError.getMessage());
        }
    });
}

private void updateCertificadoKey(final long timestamp){
    String key = String.valueOf(timestamp);
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    String url = remoteConfig.getRamaCertificados() + "/" + key;
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference ref = database.getReference(url);

    ref.child("test_child").setValue("test_value").addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if(task.isSuccessful()){
                String x = "INSERT OK!";
                Log.e("FLAG", x);
            }else{
                String x = task.getException().getMessage();
                Log.e("ERROR", x);
            }
        }
    });

}

4 个答案:

答案 0 :(得分:0)

updateCertificandoKey()方法中,而不是DatabaseReference ref = database.getReference(url);

尝试DatabaseReference ref = database.getReference().child(url);

答案 1 :(得分:0)

最后我找到了一个解决方案,区别在于setValue()方法中的参数包含了类似的参数。在调试模式下,onComplete(...)内部的条件不会执行(奇怪),但是当de app运行时,它会在android监视器中显示日志消息。代码是:

    String key = String.valueOf(timestamp);
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    String certifDate = DataFormatter.simpleDateFromMillis(timestamp);

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference databaseReference = database.getReference();
    DatabaseReference dataRef = databaseReference.child(remoteConfig.getRamaCertificados()).
            child(key).child(uid);
    dataRef.setValue(certifDate, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
            int x = 0;
            if (databaseError != null) {
                Log.e("ERROR", "Data could not be saved " + databaseError.getMessage());
            } else {
                Log.e("FLAG", "Data saved successfully.");
            }
        }
    });

答案 2 :(得分:0)

以下是带有硬编码值的代码:

07-02 00:11:38.370 3976-3976/? I/CastMediaRouteProvider: in onDiscoveryRequestChanged: request=null
07-02 00:11:38.370 3976-10676/? I/DiscoveryManager: Filter criteria(null) scannerFlags(0)
07-02 00:11:38.370 3976-10676/? I/Publisher: onUpdateFilterCriteria null
07-02 00:11:38.370 3976-10676/? E/Publisher: ProcessDatabaseInternal start
07-02 00:11:38.370 3976-10676/? W/MdnsClient_Cast: Multicast lock held. Releasing. Subtypes:"D2CA5178"
07-02 00:11:38.370 3976-6940/? I/System.out: [CDS]close[5353]
07-02 00:11:38.370 3976-10676/? W/MdnsClient: unicast receiver thread is already dead.
07-02 00:11:38.370 3976-10676/? I/DeviceScanner: [MDNS] notifyDevicesOffline: []
07-02 00:11:38.380 3976-10676/? E/Publisher: ProcessDatabaseInternal start
07-02 00:11:38.380 5087-5087/? A/MessageQueue: IdleHandler threw exception
                                               java.lang.NullPointerException
                                                   at X.1lF.b(:318521)
                                                   at X.0TN.queueIdle(:75051)
                                                   at android.os.MessageQueue.next(MessageQueue.java:207)
                                                   at android.os.Looper.loop(Looper.java:123)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5291)
                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
                                                   at dalvik.system.NativeStart.main(Native Method)
07-02 00:11:38.390 2758-2774/? E/BroadcastQueue: processNextBroadcast log test
07-02 00:11:38.390 2758-3194/? E/BroadcastQueue: processNextBroadcast log test
07-02 00:11:38.400 2758-3194/? E/BroadcastQueue: processNextBroadcast log test
07-02 00:11:38.430 3139-3873/? W/UdcContextManagerHelper: Empty context buffer. Thus might mean that the context is not synced down.
07-02 00:11:38.430 3139-3873/? W/GetDeviceDataUploadOptInStatusOp: Empty context model while retrieving upload opt-in status!
07-02 00:11:38.450 3139-3873/? W/UdcContextManagerHelper: Empty context buffer. Thus might mean that the context is not synced down.
07-02 00:11:38.450 3139-3873/? W/GetDeviceDataUploadOptInStatusOp: Empty context model while retrieving upload opt-in status!
07-02 00:11:38.480 2758-2774/? E/BroadcastQueue: processNextBroadcast log test
07-02 00:11:38.490 2758-2769/? E/BroadcastQueue: processNextBroadcast log test
07-02 00:11:42.180 2758-2774/? E/BroadcastQueue: processNextBroadcast log test
07-02 00:11:43.850 19257-19257/com.appcomfatolima I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:3 and remote module com.google.android.gms.firebase_database:6
07-02 00:11:43.850 19257-19257/com.appcomfatolima I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 6
07-02 00:11:44.040 2758-2772/? I/ActivityManager: Displayed com.appcomfatolima/.familia.FamiliaLauncherActivity: +311ms
07-02 00:11:44.220 19257-19297/com.appcomfatolima W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
07-02 00:11:44.470 19257-20236/com.appcomfatolima I/System.out: [CDS]connect[appcomfatolima.firebaseio.com/104.197.250.13:443]
07-02 00:11:44.610 19257-20236/com.appcomfatolima I/System.out: [CDS]port[59071]
07-02 00:11:45.100 19257-20236/com.appcomfatolima I/System.out: [CDS]close[59071]
07-02 00:11:45.100 19257-20236/com.appcomfatolima I/System.out: close [socket][/0.0.0.0:59071]
07-02 00:11:45.260 19257-20322/com.appcomfatolima I/System.out: [CDS]connect[s-usc1c-nss-121.firebaseio.com/104.154.130.226:443]
07-02 00:11:45.400 19257-20322/com.appcomfatolima I/System.out: [CDS]port[57257]
07-02 00:11:46.290 19257-20322/com.appcomfatolima I/System.out: [CDS]close[57257]
07-02 00:11:46.290 19257-20322/com.appcomfatolima I/System.out: close [socket][/0.0.0.0:57257]
07-02 00:11:47.080 19257-19297/com.appcomfatolima W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
07-02 00:11:47.080 19257-19297/com.appcomfatolima W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
07-02 00:11:47.100 3976-10676/? I/AuthChimeraService: Executing request: ProxyRequest[ url: https://securetoken.googleapis.com/v1/token?alt=proto&key=AIzaSyB3RytXFI5qcnIsLFFOo_fVJ4SkUeuzo-Y, method: 1 ]
07-02 00:11:47.120 3976-10676/? I/AuthChimeraService: Executing send connection operation
07-02 00:11:47.130 3976-23086/? W/FirebaseAuth: [PhoneNumberAuthPostProcessor] postProcess starts
07-02 00:11:47.130 3976-23086/? W/FirebaseAuth: [PhoneNumberAuthPostProcessor] postProcess ends
07-02 00:11:47.130 19257-20483/com.appcomfatolima I/System.out: [CDS]connect[s-usc1c-nss-121.firebaseio.com/104.154.130.226:443]
07-02 00:11:47.270 19257-19257/com.appcomfatolima E/FLAG: ******** dataRef: https://appcomfatolima.firebaseio.com/certificados/key_example/uid_example
07-02 00:11:47.270 19257-19257/com.appcomfatolima E/FLAG: ******** BEFORE SETVALUE, certifDate: 2017-07-02
07-02 00:11:47.280 19257-19257/com.appcomfatolima E/FLAG: AFTER SETVALUE
07-02 00:11:47.280 19257-20483/com.appcomfatolima I/System.out: [CDS]port[56238]
07-02 00:11:47.850 19257-20541/com.appcomfatolima I/FirebaseCrash: Sending crashes
07-02 00:11:48.070 19257-20483/com.appcomfatolima I/System.out: [CDS]close[56238]
07-02 00:11:48.070 19257-20483/com.appcomfatolima I/System.out: close [socket][/0.0.0.0:56238]
07-02 00:11:48.530 3976-23086/? I/AuthChimeraService: Executing request: ProxyRequest[ url: https://securetoken.googleapis.com/v1/token?alt=proto&key=AIzaSyB3RytXFI5qcnIsLFFOo_fVJ4SkUeuzo-Y, method: 1 ]
07-02 00:11:48.530 3976-23086/? I/AuthChimeraService: Executing send connection operation
07-02 00:11:48.540 3976-23086/? W/FirebaseAuth: [PhoneNumberAuthPostProcessor] postProcess starts
07-02 00:11:48.540 3976-23086/? W/FirebaseAuth: [PhoneNumberAuthPostProcessor] postProcess ends
07-02 00:11:48.540 19257-20594/com.appcomfatolima I/System.out: [CDS]connect[s-usc1c-nss-121.firebaseio.com/104.154.130.226:443]
07-02 00:11:48.680 19257-20594/com.appcomfatolima I/System.out: [CDS]port[36251]
07-02 00:11:48.860 15842-15941/? I/Finsky: [909] com.google.android.finsky.g.d.a(24): Completed 0 account content syncs with 0 successful.
07-02 00:11:48.860 15842-15842/? I/Finsky: [1] com.google.android.finsky.services.e.a(5): Installation state replication succeeded.
07-02 00:11:49.440 19257-20594/com.appcomfatolima I/System.out: [CDS]close[36251]
07-02 00:11:49.440 19257-20594/com.appcomfatolima I/System.out: close [socket][/0.0.0.0:36251]
07-02 00:11:50.400 3976-10676/? I/AuthChimeraService: Executing request: ProxyRequest[ url: https://securetoken.googleapis.com/v1/token?alt=proto&key=AIzaSyB3RytXFI5qcnIsLFFOo_fVJ4SkUeuzo-Y, method: 1 ]
07-02 00:11:50.400 3976-10676/? I/AuthChimeraService: Executing send connection operation
07-02 00:11:50.410 3976-23086/? W/FirebaseAuth: [PhoneNumberAuthPostProcessor] postProcess starts
07-02 00:11:50.410 3976-23086/? W/FirebaseAuth: [PhoneNumberAuthPostProcessor] postProcess ends
07-02 00:11:50.410 19257-20745/com.appcomfatolima I/System.out: [CDS]connect[s-usc1c-nss-121.firebaseio.com/104.154.130.226:443]
07-02 00:11:50.500 2917-2917/? I/wpa_supplicant: wlan0: HEART-BEAT-ACK: 496
07-02 00:11:50.560 19257-20745/com.appcomfatolima I/System.out: [CDS]port[42825]
07-02 00:11:51.360 19257-20745/com.appcomfatolima I/System.out: [CDS]close[42825]
07-02 00:11:51.360 19257-20745/com.appcomfatolima I/System.out: close [socket][/0.0.0.0:42825]
07-02 00:11:52.680 3976-23086/? I/AuthChimeraService: Executing request: ProxyRequest[ url: https://securetoken.googleapis.com/v1/token?alt=proto&key=AIzaSyB3RytXFI5qcnIsLFFOo_fVJ4SkUeuzo-Y, method: 1 ]
07-02 00:11:52.690 3976-23086/? I/AuthChimeraService: Executing send connection operation
07-02 00:11:52.700 19257-20923/com.appcomfatolima I/System.out: [CDS]connect[s-usc1c-nss-121.firebaseio.com/104.154.130.226:443]
07-02 00:11:52.700 3976-23086/? W/FirebaseAuth: [PhoneNumberAuthPostProcessor] postProcess starts
07-02 00:11:52.700 3976-23086/? W/FirebaseAuth: [PhoneNumberAuthPostProcessor] postProcess ends
07-02 00:11:52.840 19257-20923/com.appcomfatolima I/System.out: [CDS]port[37260]

这是带有setLogLevel的日志(详细,没有过滤器),我已经放了标志:

50px

答案 3 :(得分:0)

您只需要将firebase.database版本更新为最新版本。 我也一样。