I would like to find the data of all the users and sum of their scores on particular questions and find the highest rank
Now I can walk through the data users data but not through the questions node.
How do read all the data
This is how I have created my POJO :
private String id;
private String name;
private String contact;
private String email;
private String password;
private int score;
private String dob;
private String profession;
private String random;
private String status;
private String fbId;
private UserPuzzleDetails userPuzzleDetails;
public User() {
}
public User(String id, String name, String contact, String email, String password, int score,
String dob, String profession, String random, String status, String facebookid, UserPuzzleDetails userPuzzleDetails1) {
this.id = id;
this.name = name;
this.contact = contact;
this.email = email;
this.password = password;
this.score = score;
this.dob = dob;
this.profession = profession;
this.random = random;
this.status = status;
this.fbId = facebookid;
this.userPuzzleDetails = userPuzzleDetails1;
}
public User(String id, String name, String contact, String email, String password, int score,
String dob, String profession, String random, String status, String facebookid) {
this.id = id;
this.name = name;
this.contact = contact;
this.email = email;
this.password = password;
this.score = score;
this.dob = dob;
this.profession = profession;
this.random = random;
this.status = status;
this.fbId = facebookid;
}
public User(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getContact() {
return contact;
}
public String getDob() {
return dob;
}
public String getProfession() {
return profession;
}
public String getRandom() {
return random;
}
public UserPuzzleDetails getUserPuzzleDetails() {
return userPuzzleDetails;
}
public void setUserPuzzleDetails(UserPuzzleDetails questions) {
this.userPuzzleDetails = questions;
}
public String getStatus() {
return status;
}
public String getFbId() {
return fbId;
}
public void setFbId(String fbId) {
this.fbId = fbId;
}
Now I have used this function to retrieve the data but it is not logging.
How should I go by this data iterate through it.
private void getAllRank() {
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("users");
final Query query = databaseReference.orderByChild("score").limitToLast(10);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
userlist = new ArrayList<>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
User score = postSnapshot.getValue(User.class);
userlist.add(new User(score.getId(), score.getName(), score.getContact(), score.getEmail(),
score.getPassword(), score.getScore(), score.getProfession(), score.getDob(), score.getRandom(), score.getStatus(), score.getFbId(),score.getUserPuzzleDetails()));
Log.e(TAG, "onDataChange: "+userlist );
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Related
I am trying to delete the data from my DynamoDB table based on the Id(HashKey).
Association.java
#DynamoDBTable(tableName = "Association")
public class Association {
private String id;
private String name;
private String adminName;
private String email;
private String url;
private String contactNumber;
private String password;
public Association() { }
public Association(String name, String adminName, String email, String url,
String contactNumber, String password) {
this.name = name;
this.adminName = adminName;
this.email = email;
this.url = url;
this.contactNumber = contactNumber;
this.password = password;
}
public Association(String id, String name, String adminName, String email, String url,
String contactNumber, String password) {
this.id = id;
this.name = name;
this.adminName = adminName;
this.email = email;
this.url = url;
this.contactNumber = contactNumber;
this.password = password;
}
#DynamoDBHashKey(attributeName = "Id")
#DynamoDBAutoGeneratedKey
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#DynamoDBAttribute(attributeName="Name")
public String getName() { return name; }
public void setName(String name) { this.name = name; }
#DynamoDBAttribute(attributeName="AdminName")
public String getAdminName() { return adminName; }
public void setAdminName(String adminName) { this.adminName = adminName; }
#DynamoDBAttribute(attributeName="Email")
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
#DynamoDBAttribute(attributeName="Url")
public String getUrl() { return url; }
public void setUrl(String url) { this.url = url; }
#DynamoDBAttribute(attributeName="ContactNumber")
public String getContactNumber() { return contactNumber; }
public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber; }
#DynamoDBAttribute(attributeName="Password")
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
}
AssociationRepository.java:-
private AmazonDynamoDBClient getDynamoDBClient(){
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
client.setRegion(Region.getRegion(REGION));
client.setEndpoint(EndPoint);
return client;
}
private void deleteAssociation(String id) {
try {
DynamoDBConfig dynamoDBConfig = new DynamoDBConfig();
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBConfig.getDBClient());
Association associationToDelete = new Association();
associationToDelete.setId(id);
// Delete the item.
mapper.delete(associationToDelete);
} catch (Exception exception) {
System.out.println(exception);
throw exception;
}
}
I am getting the below error:-
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: No method annotated with interface com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey for class class Association
I have searched the AWS docs for this but could not find any solution that fixes this.Does anyone of you ever faced this issue?
The issue is the same I mentioned on your other post, in DynamoDB you can’t work an item using attributes which are not the primary key (Partition Key only in your scenario).
Moreover, to delete an item it is mandatory to do it by its primary key, so in your case the attribute Name.
Try to do same operation by setting the name, the error is not very descriptive so maybe there is another underlying issue. However, if you don’t meet above requirement it will never work.
I am facing the following error com.google.firebase.database.DatabaseException: Can't convert an object of type java.lang.String to type
I have a problem with:
members = ds.getValue(Members.class);
listdata.add(members);
Help me solve this error.
reference = fbd.getReference().child("Members");
reference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
members = new Members();
members = ds.getValue(Members.class);
listdata.add(members);
}
recyclerView.setAdapter(adpter);
adpter.notifyDataSetChanged();
}
Here Model Class
package com.example.positivethinkers;
public class Members {
String member_name;
String father_name;
String mobile;
String email;
String nid;
String address;
String password;
String balance;
String notify;
String occupation;
String prolink;
String nick;
public Members() {
}
public Members(String member_name, String mobile, String email, String prolink) {
this.member_name = member_name;
this.mobile = mobile;
this.email = email;
this.prolink = prolink;
}
public Members(String member_name, String father_name, String mobile, String email, String nid, String address, String password, String balance, String notify, String occupation, String prolink, String nick) {
this.member_name = member_name;
this.father_name = father_name;
this.mobile = mobile;
this.email = email;
this.nid = nid;
this.address = address;
this.password = password;
this.balance = balance;
this.notify = notify;
this.occupation = occupation;
this.prolink = prolink;
this.nick = prolink;
}
public String getMember_name() {
return member_name;
}
public void setMember_name(String member_name) {
this.member_name = member_name;
}
public String getFather_name() {
return father_name;
}
public void setFather_name(String father_name) {
this.father_name = father_name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNid() {
return nid;
}
public void setNid(String nid) {
this.nid = nid;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBalance() {
return balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
public String getNotify() {
return notify;
}
public void setNotify(String notify) {
this.notify = notify;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public String getProlink() {
return prolink;
}
public void setProlink(String prolink) {
this.prolink = prolink;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
}
Here My Database
You are getting the following error:
Can't convert an object of type java.lang.String to type com.example.positivethinkers.Members
Because you are looping through the dataSnapshot object which contains String properties and not Members objects. When you add a ChildEventListener on the Members node, onChildAdded() method is called for each and every child that exists within that node. So there is no need for an iteration. To solve this, simply remove that for loop like in the following lines of code:
reference = fbd.getReference().child("Members");
reference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
members = new Members();
members = ds.getValue(Members.class);
listdata.add(members);
adpter.notifyDataSetChanged();
}
});
Before that, create the adapter with an empty list and inside the callback just notify it about the changes.
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
members = new Members();
String name = ds.child("member_name").getValue(String.class);
String email = ds.child("email").getValue(String.class);
String mobile = ds.child("mobile").getValue(String.class);
String profile = ds.child("prolink").getValue(String.class);
Log.d("TAG", name + " / "+email+" / "+mobile+" / "+profile);
Members members = new Members(name, mobile, email, profile);
listdata.add(members);
}
}
This is the screenshot of my firestore structure
I want to get the data from my model stored in my firestore documents to my local variable
final EmployeeModel model = new EmployeeModel();
firebaseFirestore.collection("USERS").document(firebaseUser.getUid()).get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.exists()){
String username = documentSnapshot.getString(model.getUsername());
String email = documentSnapshot.getString(model.getEmail());
String location = documentSnapshot.getString(model.getLocation());
String phonenumber = documentSnapshot.getString(model.getPhonenumber());
String genderS = documentSnapshot.getString(model.getGender());
// Map<String, Object> userInfo = documentSnapshot.getData();
fragmentname.setText(username);
welcomeUser.setText(email);
employeeID.setText("1610225");
city.setText(location);
number.setText(phonenumber);
gender.setText(genderS);
} else {
Toast.makeText(getActivity(), "Document does not exist", Toast.LENGTH_SHORT).show();
}
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getActivity(), "Error ", Toast.LENGTH_SHORT).show();
Log.d(TAG, e.toString());
}
});
please correct my syntax I'm really new at this
This is EmployeeModel.class
And please show how to pass the .getUsername to a variable String username
public class EmployeeModel {
public static final String MFIELD_USERNAME = "username";
public static final String MFIELD_EMAIL = "email";
public static final String MFIELD_PASSWORD = "password";
public static final String MFIELD_LOCATION = "location";
public static final String MFIELD_PHONENUMBER = "phonenumber";
public static final String MFIELD_GENDER = "gender";
public static final String MFIELD_TYPE = "type";
private String username;
private String email;
private String password;
private String location;
private String phonenumber;
private String gender;
private String type;
private #ServerTimestamp Date timestamp;
public EmployeeModel() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public EmployeeModel(String username, String email, String password, String location, String phonenumber, String gender, String type, Date timestamp) {
this.username = username;
this.email = email;
this.password = password;
this.location = location;
this.phonenumber = phonenumber;
this.gender = gender;
this.type = type;
this.timestamp = timestamp;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}}
This is my EmployeeModel.class
and my Firestore screenshot, please help! Thank you
what you are missing is to get the model from the document
if(documentSnapshot.exists()){
EmployeeModel model = documentSnapshot.toObject(EmployeeModel.class);
//
just add the line after if statement to your existing code the rest seems fine.
I try to save data to object user by gson but have an error:
java.lang.RuntimeException: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated string at line 1 column 911330 path $.assignedUser....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
at com.loopj.andro
User class is:
public class User {
#SerializedName("id")
int id;
#SerializedName("frontName")
String name;
#SerializedName("email")
String email;
#SerializedName("phoneNumber")
String phoneNumber;
#SerializedName("pesel")
String pesel;
#SerializedName("readableAdress")
String adress;
#SerializedName("avatar")
String avatar;
#SerializedName("city")
String city;
}
and code where I use gson:
User user = new User();
String response = new String(responseBody, "UTF-8");
Gson gson = new Gson();
user = gson.fromJson(response, User.class);
Problem is in the structure of the string response?
It seems that your JSON string is malformed. You can try this tool to validate it before parsing it with GSON.
https://jsonformatter.curiousconcept.com/
User Class:
public class UserData {
private int id;
private String name;
private String email;
private String phoneNumber;
private String pesel;
private String address;
private String avatar;
private String city;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPesel() {
return pesel;
}
public void setPesel(String pesel) {
this.pesel = pesel;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
Now parse it like this:
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
Gson gson = new Gson();
response = gson.fromJson(br, User.class);
I am developing an Online Book Library application in struts...
I have a user form wherein user will enter his details like first name,last name etc and also there would be a books list where he will select some books that he wants..an d later on I want to insert those details into 2 tables..i.e user details in obl_users and and books that user select in users_books.
I used the below code for the list ..
<%
if(request.getAttribute("booksNameList") != null) {
%>
<html:select property="displayBooks" multiple="true" size="5">
<logic:iterate id="booksNameList" name="booksNameList" scope="request">
<html:option value="${booksNameList.bookId}" ><bean:write name="booksNameList" property="longTitle" /></html:option>
</logic:iterate>
</html:select>
<%
}
%>
Initially when user request form then the form will have pre populated list will all the books name in database..I am not sure about the code that i wrote to get book id in the value part of i.e value="${booksNameList.bookId}" ..
In my addUser() I want to iterate through the user selected books like this..
for (int i = 0; i < selectedBooks.length; i++) {
//insertBooks.setInt(1, generatedKeys.getInt(1));
//insertBooks.setInt(2, Integer.parseInt(selected[i]));
//insertBooks.addBatch();
}
but for that how to get the user selected books..
Here is my user.java
public class User extends ActionForm {
private int userId;
private String firstName;
private String lastName;
private String middleName;
private String username;
private String password;
private String contactNumber;
private String membershipNumber;
private String role;
private String email;
private String address;
private String comments;
private String dateOfBirth;
private int oblStatus;
private String createdDate;
private String updatedDate;
private String createdBy;
private String updatedBy;
private String displayBooks;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getMembershipNumber() {
return membershipNumber;
}
public void setMembershipNumber(String membershipNumber) {
this.membershipNumber = membershipNumber;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public int getOblStatus() {
return oblStatus;
}
public void setOblStatus(int oblStatus) {
this.oblStatus = oblStatus;
}
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
public String getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(String updatedDate) {
this.updatedDate = updatedDate;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}
public String getDisplayBooks() {
return displayBooks;
}
public void setDisplayBooks(String displayBooks) {
this.displayBooks = displayBooks;
}
}
book bean class:
private String bookId;
private String longTitle;
private String shortTitle;
private String isbn;
private String dateOfPublication;
private String noOfPages;
private String boundType;
private String dvdAvailability;
private String noOfAvailableCopies;
private int oblStatus;
private String createdDate;
private String updatedDate;
private String createdBy;
private String updatedBy;
private String displayAuthors;
private int[] authorIds;
please guide me...i am totally new to struts
You have to put "displayBooks" property as a String array like;
private String[] displayBooks;
Since you want to select multiple values on the form, Array or ArrayList can be the only better option to use.