如何从firebase JSONObject响应中检索数据

时间:2017-11-28 07:44:29

标签: android firebase

如何检索uid2

"myChat" : {
"LVaLUQ9eN8PmkazanpXzL7QdP573" : {
  "f4cTqaXf81R5dTI5AV5pMWLIjoB2" : {
    "Massige" : {
      "-KzyFATWHPh81MqAMiI7" : {
        "author" : "mohammed",
        "authorL" : "mohammed",
        "text" : "hi",
        "uid" : "LVaLUQ9eN8PmkazanpXzL7QdP573",
        "urlPhoto" : "https://firebasestorage.googleapis.com/v0/b/-acf07.appspot.com/o/LVaLUQ9eN8PmkazanpXzL7QdP573%2FmyImgebackgroun%2Fbaground?alt=media&token=744484a1-339b-4bc4-889a-019fb85854fa"
      },
    },
    "id" : {
      "uid2" : "mohammed"
    }
  }
},
"f4cTqaXf81R5dTI5AV5pMWLIjoB2" : {
  "LVaLUQ9eN8PmkazanpXzL7QdP573" : {
    "Massige" : {
      "-KzyFATbNXQLpQhIofau" : {
        "author" : "mohammed",
        "authorL" : "mohammed",
        "text" : "hi",
        "uid" : "LVaLUQ9eN8PmkazanpXzL7QdP573",
        "urlPhoto" : "https://firebasestorage.googleapis.com/v0/b/-acf07.appspot.com/o/LVaLUQ9eN8PmkazanpXzL7QdP573%2FmyImgebackgroun%2Fbaground?alt=media&token=744484a1-339b-4bc4-889a-019fb85854fa"
      },
    },
    "id" : {
      "uid2" : "mohammed"
    }
  }
 }
 },

这个代码我试图检索但没有工作。

    final TextView text=(TextView)findViewById(R.id.textView26);

    reference = FirebaseDatabase.getInstance().getReference("myChat");
    mAuth = FirebaseAuth.getInstance();
    String currentUser = mAuth.getCurrentUser().getUid();


    reference.child(currentUser).child("id").addValueEventListener(new ValueEventListener() {


        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            final String name = dataSnapshot.child("uid2").getValue().toString();
            text.setText(name);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

错误日志

  

E / AndroidRuntime:FATAL EXCEPTION:main,PID:9410   java.lang.NullPointerException:尝试调用虚方法   ' java.lang.String java.lang.Object.toString()'在null对象上   参考                                                                                           在com.google.android.gms.internal.zzbpx.zza(未知来源)                                                                                           在com.google.android.gms.internal.zzbqx.zzZT(未知来源)                                                                                           在com.google.android.gms.internal.zzbra $ 1.run(未知来源)                                                                                           在android.os.Handler.handleCallback(Handler.java:751)                                                                                           在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                           在android.os.Looper.loop(Looper.java:154)                                                                                           在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                           at java.lang.reflect.Method.invoke(Native Method)                                                                                           在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2 个答案:

答案 0 :(得分:1)

你可以使用下面的帮助类

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        Post post = dataSnapshot.getValue(Post.class);
        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
    }
};
mPostReference.addValueEventListener(postListener);

了解更多信息: https://firebase.google.com/docs/database/android/read-and-write

修改

声明一个实例(类变量)

private FirebaseAuth mAuth;
private DatabaseReference reference;

在onCreate()中初始化mAuth和数据库

reference = FirebaseDatabase.getInstance().getReference().child("users");
mAuth = FirebaseAuth.getInstance();

将当前用户ID获取为字符串

FirebaseUser currentUser = mAuth.getCurrentUser();

为下面的用户创建数据库引用

reference.child(currentUser).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final String name = dataSnapshot.child("id").getValue().toString();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

答案 1 :(得分:1)

试试这个,首先您的回复转换为JsonObject,然后解析下面给出的。

  try {
        JSONObject jsonObject = new JSONObject(result); // hear your responce
        JSONObject jsonObject1 = jsonObject.getJSONObject("LVaLUQ9eN8PmkazanpXzL7QdP573");
        JSONObject jsonObject2  = jsonObject1.getJSONObject("f4cTqaXf81R5dTI5AV5pMWLIjoB2");
        JSONObject jsonObject3 = jsonObject2.getString("id");
        String  uiod = jsonObject3.getString("uid2");

    }catch (JSONException j){
        j.printStackTrace();
    }