2016-09-26 32 views
0

我已經看過其他帖子,但似乎沒有任何解決方案符合我的問題。錯誤是在方法FirebaseRef.setValue()Firebase setvalue DatabaseException:無法用類類解析節點

我試圖將數據保存到Firebase server。起初我以爲錯誤是因爲我試圖將數據保存爲自定義對象,然後我試圖將它保存爲一個基本的散列(我跟着this鏈接教程)

protected Boolean doInBackground(Void... params) { 
      // Database Connection, if no connection or what not, exception will be here 
      mDatabase = FirebaseDatabase.getInstance().getReference(); 
      Log.d(DBTAG, mDatabase.toString()); 

      // 'child database' 
      mBooksDatabase = mDatabase.child("books"); 
      mCurrentUser = mDatabase.child("users").child(mUserEmail); 

      // address to upload the book, later we can call newBookRef.getKey() to get the ID 
      // and use the ID to indicate the relationship between the owner and the book 
      final DatabaseReference newBookRef = mBooksDatabase.push(); 
      try { 
       Map<String, String> mBookTest = new HashMap<String, String>(); 
       mBookTest.put("ISBN", "9781566199094"); 
       mBookTest.put("title", "Book of Love"); 
       newBookRef.setValue(mBookTest, new Firebase.CompletionListener() { 
        @Override 
        public void onComplete(FirebaseError firebaseError, Firebase firebase) { 
         if (firebaseError != null) { 
          Log.e(DBTAG, "Data could not be saved. " + firebaseError.getMessage()); 
         } else { 
          Log.d(DBTAG, "Data saved successfully."); 
          // update the 'owns' list in user's data 
          final String bookRef = newBookRef.getKey(); 
          mCurrentUser.child("owns").child(bookRef).setValue("1"); 
          //TODO: we can use this to count how many of the same books an user has 
         } 
        } 
       }); 
      } catch (DatabaseException e){ 
       Log.e(DBTAG, "Error occurred", e); 
      } 
      // if owner is desired in book, we can modify this part 

      return true; 
     } 

錯誤消息:

09-26 20:37:12.631 5091-5399/bookjobs.bookjobs D/DB in BookController: https://bookjobs-6c56f.firebaseio.com 
09-26 20:37:12.641 5091-5399/bookjobs.bookjobs E/DB in BookController: Error occurred 
                     com.google.firebase.database.DatabaseException: Failed to parse node with class class bookjobs.bookjobs.BookController$UploadBookTask$1 
                      at com.google.android.gms.internal.zzakk.zza(Unknown Source) 
                      at com.google.android.gms.internal.zzakk.zzbq(Unknown Source) 
                      at com.google.android.gms.internal.zzakn.zzbr(Unknown Source) 
                      at com.google.firebase.database.DatabaseReference.setValue(Unknown Source) 
                      at bookjobs.bookjobs.BookController$UploadBookTask.doInBackground(BookController.java:59) 
                      at bookjobs.bookjobs.BookController$UploadBookTask.doInBackground(BookController.java:30) 
                      at android.os.AsyncTask$2.call(AsyncTask.java:295) 
                      at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                      at java.lang.Thread.run(Thread.java:818) 
09-26 20:37:15.831 5091-5169/bookjobs.bookjobs W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found. 
+1

您不需要使用AsyncTack進行Firebase操作,Firebase已經過優化,每個網絡相關操作都在後臺線程上運行。 –

+0

我不知道如何通過編寫HashMap來獲取該錯誤消息。你可以仔細檢查一下你的代碼示例是否正確? –

回答

4

您撥打setValue()時的完成監聽器來自舊版2.xx SDK:Firebase.CompletionListener()。您必須使用新的9.x.x SDK的完成偵聽器,DatabaseReference.CompletionListener()

這兩個SDK不兼容。您應該專門使用新的SDK。更新您的build.gradle刪除:

compile 'com.firebase:firebase-client-android:2.x.x' 

有關詳細信息,請參閱Upgrade Guide

+0

趕上qbix!令人尷尬的是,邁克爾和我自己都忽略了這一點。感謝您幫助其他Firebase開發人員! –