首页 > 解决方案 > firebase.database.DatabaseException:在类上找不到要序列化的属性

问题描述

我已经关注了一些具有相同背景的问题,但我仍然没有办法理解或找到这个错误。我正在尝试将数据发送到我的 Firebase 实时数据库。付款的总费用,我的购物车中的列表和用户名,但我仍然收到此错误。

      com.google.firebase.database.DatabaseException: No properties to serialize found on class com.urbanx.urbaninsure.model.RequestDelivery
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.<init>(com.google.firebase:firebase-database@@19.1.0:530)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-database@@19.1.0:312)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database@@19.1.0:166)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToPlainJavaTypes(com.google.firebase:firebase-database@@19.1.0:60)
    at com.google.firebase.database.DatabaseReference.setValueInternal(com.google.firebase:firebase-database@@19.1.0:282)
    at com.google.firebase.database.DatabaseReference.setValue(com.google.firebase:firebase-database@@19.1.0:159)
    at com.urbanx.urbaninsure.InsurancePayment$1.onClick(InsurancePayment.java:255)

我的支付类

public class Payment extends AppCompatActivity implements View.OnClickListener {

LinearLayout InsurancePaymentLayout, MethodPaymentLayout, MpesaLayout, CreditCardLayout, EazzyPayLayout,
        mpesaLayout, CreditBarLayout, EazzyPayBarLayout, PaymentStatementLayout,LAddress,
        DeclinedPaymentStatementLayout,CompleteLayout, DeliveryLayout, hdCardLayout, cardLayout;
TextView tvCostStatement,tvCost, tvPay, tvAddress,etvAddress ;
TextView tvSerial, tvStroke, tvExpire, tvCardNumber;
RadioButton rbMpesa, rbCard, rbEazzy;
ProgressBar pbComplete;
Button bComplete, bDelivery, bPay;
ImageView ivMpesa, ivCredit;
EditText etCcn, etYear, etMonth, cd1, cd2, cd3, cd4;

List<ProposalDetails> cart = new ArrayList<>();
DatabaseHelper myDb;
CartAdapter adapter;
FirebaseDatabase database;

DatabaseReference requests;
CoordinatorLayout layout;
DatabaseReference mCustomerDatabase;
private FirebaseAuth mAuth;

private String totalPrice;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_insurance_payment);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    layout = (CoordinatorLayout)findViewById(R.id.layout);
    LAddress = (LinearLayout)findViewById(R.id.LAddress);
    etvAddress = (TextView) findViewById(R.id.etvAddress);
    tvAddress = (TextView)findViewById(R.id.tvAddress);

    bComplete = (Button)findViewById(R.id.bComplete);
    bComplete.setOnClickListener(this);

    cardLayout = (LinearLayout)findViewById(R.id.cardLayout);
    cd1 = (EditText)findViewById(R.id.cd1);
    cd2 = (EditText)findViewById(R.id.cd2);
    cd3 = (EditText)findViewById(R.id.cd3);
    cd4 = (EditText)findViewById(R.id.cd4);
    ivCredit = (ImageView)findViewById(R.id.ivCredit);
    hdCardLayout = (LinearLayout)findViewById(R.id.hdCardLayout);
    tvCardNumber = (TextView)findViewById(R.id.tvCardNumber);
    tvSerial = (TextView)findViewById(R.id.tvSerial);
    tvStroke = (TextView)findViewById(R.id.tvStroke);
    tvExpire =(TextView)findViewById(R.id.tvExpire);
    etCcn = (EditText)findViewById(R.id.etCcn);
    etMonth = (EditText)findViewById(R.id.etMonth);
    etYear = (EditText)findViewById(R.id.etYear);
    bPay = (Button)findViewById(R.id.bPay);
    ivMpesa = (ImageView)findViewById(R.id.ivMpesa);
    pbComplete = (ProgressBar)findViewById(R.id.pbComplete);
    rbMpesa = (RadioButton)findViewById(R.id.rbMpesa);
    rbMpesa.setOnClickListener(this);
    rbCard = (RadioButton)findViewById(R.id.rbCard);
    rbCard.setOnClickListener(this);
    rbEazzy = (RadioButton)findViewById(R.id.rbEazzy);
    rbEazzy.setOnClickListener(this);
    tvCostStatement = (TextView)findViewById(R.id.tvCostStatement);

     totalPrice = getIntent().getStringExtra("totalPrice");
    tvCost = (TextView)findViewById(R.id.tvCost);
    tvCost.setText(totalPrice);

    tvCost.setText(totalPrice);
    myDb = new DatabaseHelper(this);
    adapter = new CartAdapter(cart,this);
    database = FirebaseDatabase.getInstance();

    requests = database.getReference("OrderRequests");

    tvPay = (TextView)findViewById(R.id.tvPay);
    InsurancePaymentLayout = (LinearLayout)findViewById(R.id.InsurancePaymentLayout);
    MethodPaymentLayout = (LinearLayout)findViewById(R.id.MethodPaymentLayout);
    MpesaLayout = (LinearLayout)findViewById(R.id.MpesaLayout);
    CreditCardLayout = (LinearLayout)findViewById(R.id.CreditCardLayout);
    EazzyPayLayout = (LinearLayout)findViewById(R.id.EazzyPayLayout);
    mpesaLayout = (LinearLayout)findViewById(R.id.mpesaLayout);
    mpesaLayout.setVisibility(View.GONE);
    CreditBarLayout = (LinearLayout)findViewById(R.id.CreditBarLayout);
    CreditBarLayout.setVisibility(View.GONE);
    EazzyPayBarLayout = (LinearLayout)findViewById(R.id.EazzyPayBarLayout);
    EazzyPayBarLayout.setVisibility(View.GONE);
    PaymentStatementLayout = (LinearLayout)findViewById(R.id.PaymentStatementLayout);
    PaymentStatementLayout.setVisibility(View.GONE);
    DeclinedPaymentStatementLayout = (LinearLayout)findViewById(R.id.DeclinedPaymentStatementLayout);
    CompleteLayout = (LinearLayout)findViewById(R.id.CompleteLayout);
    CompleteLayout.setVisibility(View.VISIBLE);
    DeliveryLayout = (LinearLayout)findViewById(R.id.DeliveryLayout);
    bDelivery = (Button)findViewById(R.id.bDelivery);
    bDelivery.setOnClickListener(this);


}

@Override
public void onClick(View v) {
    switch (v.getId()){

        case R.id.rbMpesa:

            if (rbMpesa.isChecked()){
                rbCard.setChecked(false);
                rbEazzy.setChecked(false);
                mpesaLayout.setVisibility(View.VISIBLE);
                CreditBarLayout.setVisibility(View.GONE);
                EazzyPayBarLayout.setVisibility(View.GONE);

            }

            break;

        case R.id.rbCard:

            if (rbCard.isChecked()){

                rbMpesa.setChecked(false);
                rbEazzy.setChecked(false);
                mpesaLayout.setVisibility(View.GONE);
                CreditBarLayout.setVisibility(View.VISIBLE);
                EazzyPayBarLayout.setVisibility(View.GONE);

            }

            break;

        case R.id.rbEazzy:

            if (rbEazzy.isChecked()){

                rbMpesa.setChecked(false);
                rbCard.setChecked(false);
                mpesaLayout.setVisibility(View.GONE);
                CreditBarLayout.setVisibility(View.GONE);
                EazzyPayBarLayout.setVisibility(View.VISIBLE);

            }

            break;

        case R.id.bComplete:

            showAlertDialog();

            break;

        case R.id.bDelivery:

            startActivity(new Intent(this, DeliveryActivity.class));

            break;
    }
}

private void showAlertDialog() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Final step!");
    alertDialog.setMessage("Enter your delivery location: ");


    LayoutInflater inflater = LayoutInflater.from(this);
    View destination_layout = inflater.inflate(R.layout.enter_address,null);

    final EditText etAddress = destination_layout.findViewById(R.id.edtAddress);

    alertDialog.setView(destination_layout);


    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            final android.app.AlertDialog waitingDialog = new SpotsDialog(InsurancePayment.this,"Placing ");
            waitingDialog.show();

            if (TextUtils.isEmpty(etAddress.getText().toString()))
            {
                Snackbar.make(layout,"Please enter delivery location", Snackbar.LENGTH_SHORT)
                        .show();

                return;
            }


            String address = etAddress.getText().toString();

           DeliveryRequest requestDelivery = new DeliveryRequest();
            requestDelivery.setFirstNameProposer("am");
            requestDelivery.setLastNameProposer("");
            requestDelivery.setAddress("address");



  //         String json =  gson.toJson(requestDelivery);

            String requestId = requests.push().getKey();




            requests.child(requestId)
                    .setValue(requestDelivery);

            myDb.deleteAllInsurance();

            myDb.deleteAllInsurance();


            LAddress.setVisibility(View.VISIBLE);
            etvAddress.setText(address);

            waitingDialog.dismiss();

            Toast.makeText(InsurancePayment.this,"Thank your, Order placed",Toast.LENGTH_SHORT).show();

            PaymentStatementLayout.setVisibility(View.VISIBLE);
            CompleteLayout.setVisibility(View.GONE);
            pbComplete.setVisibility(View.VISIBLE);
            DeliveryLayout.setVisibility(View.VISIBLE);
        }
    });

    alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int which) {
            dialogInterface.dismiss();
        }
    });

    alertDialog.show();
}

@Override
public void onBackPressed() {


    if ((pbComplete.getVisibility() == View.VISIBLE)&&(DeliveryLayout.getVisibility()==View.VISIBLE)) {
        // Its visible


    } else {

        Intent openProposalForm = new Intent(InsurancePayment.this, Cart.class);
        startActivity(openProposalForm);
        finish();
        // Either gone or invisible
    }
}

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}
}

我的 RequestDelivery 类

public class DeliveryRequest implements Serializable  {


public String   firstNameProposer, lastNameProposer,address;

public DeliveryRequest() {


}
public String getFirstNameProposer() {
    return firstNameProposer;
}

public void setFirstNameProposer(String firstNameProposer) {
    this.firstNameProposer = firstNameProposer;
}

public String getLastNameProposer() {
    return lastNameProposer;
}

public void setLastNameProposer(String lastNameProposer) {
    this.lastNameProposer = lastNameProposer;
}
public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

}

标签: javaandroidfirebase-realtime-database

解决方案


我以前没有使用过 firebase 数据库,但我使用过其他 API,这些 API 可以为您提供 JSON 响应,或者我向服务器发送 json 响应。因此,当您获取/发送 JSON 响应时,您需要告诉 Java 如何使用@SerializedName. 例如,如果您收到这样的 json 响应:

{
    phone:"12345678",
    __total:"1000",
    ... ...
}

我认为在这种情况下,您需要RequestDelivery像这样在类中进行更改:

class RequestDelivery{
@SerializedName("phone")  /* here this 'phone' is the string that is exactly in the json response*/
public String phone;
@SerializedName("__total") /* put __total because it is in my sample json. You change it according to your actual json response */
public String total;
... ... ...
}

请谷歌了解更多关于你应该为列表做什么的细节(你的代码中的proposalDetails)。我希望这行得通。祝你好运。


推荐阅读