Firebase - 用户身份验证,我的 userRef 不起作用
Firebase - User authentication, my userRef won't work
我正在尝试向我的应用程序添加用户身份验证,但我不明白为什么我无法通过我的 FirebaseRecyclerAdapter 中的以下 ref 访问特定用户。
waypointsUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/";
final Firebase userRef = new Firebase(waypointsUrl);
FATAL EXCEPTION:
main Process: com.example.rasmusjosefsson.rjcar, PID: 7797
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161)
at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150)
at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1170)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Ha
我的 JSON 结构。
还想知道是否可以使用 "Date added: 2016-05-05 03:50:02" : 作为密钥?
{
"users" : {
"0057242b-81e2-4f97-bca7-b671212614ba" : {
"email" : "kalle@hotmail.se",
"waypoints" : {
"Date added: 2016-05-05 03:50:02" : {
"-KH-eHHxVpf--IcnYgIq" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eK6z9vIefgcI5Pq2" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eMIl8zB_Yq67SY69" : {
"latitude" : 123,
"longitude" : 123
}
}
}
},
"1a96a633-7e67-41b8-9aa7-c70d4b7eb59c" : {
"email" : "josefssons@hotmail.se",
"waypoints" : {
"Date added: 2016-05-05 03:46:21" : {
"-KH-bcrZwOR2c3Ouc-hv" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-bhdzAatR84pcX1_h" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-dz0jw_UTsG2ojFJM" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-e0qLa2fY1fsFpiTS" : {
"latitude" : 123,
"longitude" : 123
}
},
"Date added: 2016-05-05 03:51:09" : {
"-KH-eXXo5OGSG7GIisMf" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eZPi1spUbFUwkmfh" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eZSt613mKv1cJyox" : {
"latitude" : 123,
"longitude" : 123
}
},
我想我没看到这里的某处是错误的。
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyLatLngBack2 {
@JsonProperty("email")
private String email;
@JsonProperty("waypoints")
private TreeMap<String, Waypoints> waypoints;
public MyLatLngBack2(String email, TreeMap<String, WaypointsBack2> waypoints) {
this.email = email;
this.waypoints = waypoints;
}
public MyLatLngBack2() {}
public String getEmail() {
return email;
}
public TreeMap<String, WaypointsBack2> getWaypoints() {
return waypoints;
}
public void setWaypoints(TreeMap<String, WaypointsBack2> waypoints) {
this.waypoints = waypoints;
}
}
waypoints数据
@JsonIgnoreProperties(ignoreUnknown = true)
public class WaypointsBack2 {
@JsonProperty("longitude")
private double longitude;
@JsonProperty("latitude")
private double latitude;
public WaypointsBack2(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public WaypointsBack2() {
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
}
这是剩余的代码
mRef = new Firebase(Constants.FIREBASE_URL);
if (mRef.getAuth() == null) {
loadLoginView();
}
try {
mUserId = mRef.getAuth().getUid();
} catch (Exception e) {
loadLoginView();
}
mRecyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
mRecyclerView.setHasFixedSize(false);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setReverseLayout(false);
mRecyclerView.setLayoutManager(manager);
// https://todoapprj.firebaseio.com/users/1a96a633-7e67-41b8-9aa7-c70d4b7eb59c
waypointsUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/";
final Firebase userRef = new Firebase(waypointsUrl);
// (LatLngViewHolder.class, userRef) gives me bounce to blaha
mRecycleViewAdapter = new FirebaseRecyclerAdapter<MyLatLngBack2, LatLngViewHolder>(MyLatLngBack2.class, R.layout.list_item, LatLngViewHolder.class, mRef) {
@Override
public void populateViewHolder(LatLngViewHolder latLngViewHolder, MyLatLngBack2 myLatLng, int position) {
// gets me the root email (wrong one)
String email = myLatLng.getEmail();
}
};
mRecyclerView.setAdapter(mRecycleViewAdapter);
}
我找到了弹跳失败的解决方案,我确实将 MyLatLng POJO 更改为以下内容:
@JsonIgnoreProperties(ignoreUnknown = true)
public class LatLngsModel {
public Map<String, Map <String, MyWaypoint>> latLngs;
public LatLngsModel() {
}
public LatLngsModel(Map<String, Map <String, MyWaypoint>> myLatLngs) {
this.latLngs = myLatLngs;
}
public Map<String, Map <String, MyWaypoint>> getMyWaypoints() {
return latLngs;
}
}
我正在尝试向我的应用程序添加用户身份验证,但我不明白为什么我无法通过我的 FirebaseRecyclerAdapter 中的以下 ref 访问特定用户。
waypointsUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/";
final Firebase userRef = new Firebase(waypointsUrl);
FATAL EXCEPTION: main Process: com.example.rasmusjosefsson.rjcar, PID: 7797 com.firebase.client.FirebaseException: Failed to bounce to type at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185) at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161) at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150) at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122) at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1170) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Ha
我的 JSON 结构。 还想知道是否可以使用 "Date added: 2016-05-05 03:50:02" : 作为密钥?
{
"users" : {
"0057242b-81e2-4f97-bca7-b671212614ba" : {
"email" : "kalle@hotmail.se",
"waypoints" : {
"Date added: 2016-05-05 03:50:02" : {
"-KH-eHHxVpf--IcnYgIq" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eK6z9vIefgcI5Pq2" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eMIl8zB_Yq67SY69" : {
"latitude" : 123,
"longitude" : 123
}
}
}
},
"1a96a633-7e67-41b8-9aa7-c70d4b7eb59c" : {
"email" : "josefssons@hotmail.se",
"waypoints" : {
"Date added: 2016-05-05 03:46:21" : {
"-KH-bcrZwOR2c3Ouc-hv" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-bhdzAatR84pcX1_h" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-dz0jw_UTsG2ojFJM" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-e0qLa2fY1fsFpiTS" : {
"latitude" : 123,
"longitude" : 123
}
},
"Date added: 2016-05-05 03:51:09" : {
"-KH-eXXo5OGSG7GIisMf" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eZPi1spUbFUwkmfh" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eZSt613mKv1cJyox" : {
"latitude" : 123,
"longitude" : 123
}
},
我想我没看到这里的某处是错误的。
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyLatLngBack2 {
@JsonProperty("email")
private String email;
@JsonProperty("waypoints")
private TreeMap<String, Waypoints> waypoints;
public MyLatLngBack2(String email, TreeMap<String, WaypointsBack2> waypoints) {
this.email = email;
this.waypoints = waypoints;
}
public MyLatLngBack2() {}
public String getEmail() {
return email;
}
public TreeMap<String, WaypointsBack2> getWaypoints() {
return waypoints;
}
public void setWaypoints(TreeMap<String, WaypointsBack2> waypoints) {
this.waypoints = waypoints;
}
}
waypoints数据
@JsonIgnoreProperties(ignoreUnknown = true)
public class WaypointsBack2 {
@JsonProperty("longitude")
private double longitude;
@JsonProperty("latitude")
private double latitude;
public WaypointsBack2(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public WaypointsBack2() {
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
}
这是剩余的代码
mRef = new Firebase(Constants.FIREBASE_URL);
if (mRef.getAuth() == null) {
loadLoginView();
}
try {
mUserId = mRef.getAuth().getUid();
} catch (Exception e) {
loadLoginView();
}
mRecyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
mRecyclerView.setHasFixedSize(false);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setReverseLayout(false);
mRecyclerView.setLayoutManager(manager);
// https://todoapprj.firebaseio.com/users/1a96a633-7e67-41b8-9aa7-c70d4b7eb59c
waypointsUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/";
final Firebase userRef = new Firebase(waypointsUrl);
// (LatLngViewHolder.class, userRef) gives me bounce to blaha
mRecycleViewAdapter = new FirebaseRecyclerAdapter<MyLatLngBack2, LatLngViewHolder>(MyLatLngBack2.class, R.layout.list_item, LatLngViewHolder.class, mRef) {
@Override
public void populateViewHolder(LatLngViewHolder latLngViewHolder, MyLatLngBack2 myLatLng, int position) {
// gets me the root email (wrong one)
String email = myLatLng.getEmail();
}
};
mRecyclerView.setAdapter(mRecycleViewAdapter);
}
我找到了弹跳失败的解决方案,我确实将 MyLatLng POJO 更改为以下内容:
@JsonIgnoreProperties(ignoreUnknown = true)
public class LatLngsModel {
public Map<String, Map <String, MyWaypoint>> latLngs;
public LatLngsModel() {
}
public LatLngsModel(Map<String, Map <String, MyWaypoint>> myLatLngs) {
this.latLngs = myLatLngs;
}
public Map<String, Map <String, MyWaypoint>> getMyWaypoints() {
return latLngs;
}
}