Issue
I am trying to insert user data into the real-time database(Firebase). But when I run the app on an emulator or on my phone it just keeps loading(I set up a loadingBar to dismiss when a response is received) I tried going through the logcat to find an error message I got this
Fetching config failed. code, error: 0, java.net.UnknownHostException: Unable to resolve host "app-measurement.com": No address associated with hostname: com.google.android.gms.measurement.internal.zzeq.run(com.google.android.gms:play-services-measurement@@20.0.0:26)
I checked the rules, the dependencies looked through the web I can't seem to find what I missed.
I thought I should mention the first time I tried using firebase I faced the same problem on a less serious project so I didn't sort it out. That time I was using FIrebaseAuth so I decided to use a different approach this time. here is the signIn code.
private void allowAccessAccount(String phoneNumber, String password) {
final DatabaseReference rootRef;
rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(PARENT_DB_NAME).child(phoneNumber).exists()) {
User userData = dataSnapshot.child(PARENT_DB_NAME).child(phoneNumber)
.getValue(User.class);
if (userData.getPhone().equals(phoneNumber)) {
if (userData.getPassword().equals(password)) {
if (PARENT_DB_NAME.equals("Admins")) {
Toast.makeText(SignInActivity.this, "Success", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
//Open Home Activity
startActivity(new Intent(SignInActivity.this, AdminPanel.class));
finish();
} else if (PARENT_DB_NAME.equals("Users")) {
Toast.makeText(SignInActivity.this, "Success", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
//Get User Data
Prevalent.currentOnlineUsr = userData;
//Open Home Activity
startActivity(new Intent(SignInActivity.this, HomeActivity.class));
finish();
}
} else {
loadingBar.dismiss();
inputPassword.setError("Incorrect password");
}
}
} else {
inputPhoneNumber.setError("Not registered");
loadingBar.dismiss();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(SignInActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
Here is the signUp code
private void signUpUser(String username, String phoneNumber, String password) {
final DatabaseReference rootRef;
rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!(dataSnapshot.child("Users").child(phoneNumber).exists())){
HashMap<String, Object> userDataMap = new HashMap<>();
userDataMap.put("username", username);
userDataMap.put("phone", phoneNumber);
userDataMap.put("password", password);
rootRef.child("Users").child(phoneNumber).updateChildren(userDataMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
//Account creation successful
Toast.makeText(SignUpActivity.this, "Success", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
//Open Sign In Activity
startActivity(new Intent(SignUpActivity.this,SignInActivity.class));
}
else {
//Account creation Failed
loadingBar.dismiss();
Toast.makeText(SignUpActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
});
} else {
inputPhoneNumber.setError("Already registered");
loadingBar.dismiss();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
Solution
I don't know exactly what went wrong as I ran through every little thing over 5 times looking for where I went wrong. I fixed it by removing firebase and letting the assistant add it for me. I also removed the Jcenter repository plus I changed the email I was using for android studio to correspond with the email I was using for the project.
Answered By - Bryne Phiri
Answer Checked By - David Goodson (JavaFixing Volunteer)