无法将项目添加到Firebase数据库

时间:2018-04-22 15:19:53

标签: android firebase firebase-realtime-database

我创建了一个活动,我可以从中添加产品标题,描述,价格和图像到数据库,但是当我点击提交按钮时没有任何反应。我已获得互联网许可,启用了Firebase身份验证(电子邮件/密码和Google)。

我找不到任何错误,在logcat中我找到了这些,但我不知道如何解决:

DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@f09391f
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.

数据库规则也是:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

和存储规则是:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

我的活动代码是:

public class createPost extends AppCompatActivity {

    private ImageButton m_SelectImage;
    private EditText mProdTitle;
    private EditText mProdDesc;
    private EditText mProdPrice;
    private Button mSubmit;
    private static final int GALLERY_REQUEST = 1;
    private Uri mImageUri = null;
    private StorageReference mStorage;
    private DatabaseReference mDatabase;
    private ProgressDialog mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_post);
        m_SelectImage = (ImageButton)findViewById(R.id.p_image);
        mProdTitle = (EditText)findViewById(R.id.p_title);
        mProdDesc = (EditText)findViewById(R.id.p_description);
        mProdPrice = (EditText)findViewById(R.id.p_price);
        mSubmit = (Button)findViewById(R.id.p_submit);

        mStorage = FirebaseStorage.getInstance().getReference();
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Products");

        mProgress = new ProgressDialog(this);

        m_SelectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent,GALLERY_REQUEST);
            }
        });
        mSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPosting();
            }
        });
    }

    private void startPosting() {
        mProgress.setMessage("Adding new Post");
        final String title_val = mProdTitle.getText().toString().trim();
        final String desc_val= mProdDesc.getText().toString().trim();
        final String price_val = mProdPrice.getText().toString().trim();
        if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && TextUtils.isEmpty(price_val) && mImageUri!=null)
        {
            mProgress.show();
            StorageReference filePath = mStorage.child("Products").child(mImageUri.getLastPathSegment());
            filePath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Uri downloadURL = taskSnapshot.getDownloadUrl();
                    DatabaseReference newPost = mDatabase.push();
                    newPost.child("Title").setValue(title_val);
                    newPost.child("Description").setValue(desc_val);
                    newPost.child("Price").setValue(price_val);
                    newPost.child("Product_Image").setValue(downloadURL.toString());
                    mProgress.dismiss();
                    Toast.makeText(createPost.this,"Posting Complete",Toast.LENGTH_LONG).show();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(createPost.this,"Error Occured"+e.getMessage(),Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==GALLERY_REQUEST && resultCode==RESULT_OK)
        {
            mImageUri=data.getData();
            m_SelectImage.setImageURI(mImageUri);
        }
    }
}

另外,我在代码中找不到任何错误(如果有的话)。

2 个答案:

答案 0 :(得分:1)

当您点击提交按钮时,是否有用户登录? 您可以通过向onCreate()添加以下代码来检查:

FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            Log.d(getSubClassTAG(), "onAuthStateChanged: singed in: " + user.getUid());
        } else {
            Log.d(getSubClassTAG(), "onAuthStateChanged: signed out: ");
        }
    }
};

如果没有,您必须登录以下用户:

FirebaseAuth auth = FirebaseAuth.getInstance();
auth.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        Log.d(TAG, "signInWithEmailAndPasswort: onComplete: " + task.isSuccessful());

        if (!task.isSuccessful()) {
            Log.w(TAG, "signInWithEmailAndPassword: failed ", task.getException());
        } else {
            //signin was successful
        }
    }
});

答案 1 :(得分:1)

我发现了你的错误。一个失踪!在你的条件。改变这个

if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && !TextUtils.isEmpty(price_val) && mImageUri!=null)

到此:

system("ruby app.rb & ruby app2.rb")