I am implementing a firebase example as given in their documentations. I am facing this error:
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.XYZ.$BlogPost]: can not instantiate from JSON object (need to add/enable type information?)
Here is my code:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Firebase.setAndroidContext(this);
}
@Override
protected void onStart() {
super.onStart();
// Get a reference to our posts
Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
// Attach an listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
BlogPost post = postSnapshot.getValue(BlogPost.class);
System.out.println(post.getAuthor() + " - " + post.getTitle());
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
}
public class BlogPost {
private String author;
private String title;
public BlogPost() {
// empty default constructor, necessary for Firebase to be able to deserialize blog posts
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
}
}
I have gone through many questions on the same thing saying to include empty constructor necessary to deserialize the JSON. I have included that but still I am not able to resolve the issue. This is the JSON I am trying to deserialize:
{
"-JRHTHaIs-jNPLXOQivY":{
"author":"gracehop",
"title":"Announcing COBOL, a New Programming Language"
},
"-JRHTHaKuITFIhnj02kE":{
"author":"alanisawesome",
"title":"The Turing Machine"
}
}
I don't know what I am missing in my code. Any help regrading this is appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…