将parcelable捆绑数据发送到服务

时间:2014-05-08 08:47:26

标签: android service bundle parcelable intentservice

我正在将一个parcelable对象从片段传递给片段,并且生成的对象没问题。

但是在最后一个片段中,我将parcelable对象传递给IntentService,当我得到该对象时,该对象中的所有Parcelable对象都为null,其他值是随机的。

这是我在最后一个片段

中获取我的parcelable对象的方法
public static ValidInstallmentsFragment newInstance(CheckoutRequest checkoutRequest, Card card){

    Bundle bundle = new Bundle();
    bundle.putParcelable(BundleHelper.CHECKOUT_REQUEST, checkoutRequest);

    ValidInstallmentsFragment validInstallmentsFragment = new ValidInstallmentsFragment();
    validInstallmentsFragment.setArguments(bundle);

    return validInstallmentsFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    checkoutRequest = (CheckoutRequest) getArguments().getParcelable(BundleHelper.CHECKOUT_REQUEST);
}

之后,我将带有parcelable数据的Intent发送给IntentService

Bundle bundle = new Bundle();
bundle.putParcelable(BundleHelper.SEND_SECURE_3D_REQUEST, sendSecure3DRequest);
bundle.putParcelable(BundleHelper.CHECKOUT_REQUEST, checkoutRequest);

mCardPaymentServiceIntent = new Intent(getActivity(), CardPaymentIntentService.class);
mCardPaymentServiceIntent.putExtras(bundle);
getActivity().startService(mCardPaymentServiceIntent);

在IntentService中,我在onHandleIntent方法中获取该对象:

@Override
protected void onHandleIntent(Intent intent) {
    checkoutRequest = (CheckoutRequest) intent.getExtras().getParcelable(BundleHelper.CHECKOUT_REQUEST);
}

但是在IntentService中我尝试从CheckoutRequest对象获取一个对象BillingInformation,但它返回null。 Checkout Request对象中还有一些其他对象,但它们都是null。正常的int和String变量也会返回一些随机值。并且是的,所有这些都在实现Parcelable接口。

这是我的CheckoutRequest类:

public class CheckoutRequest implements Parcelable {

public CheckoutRequest() {
}

private String email;

private List<Product> products;

private BillingInformation billingInformation;

private ShippingMethod shippingMethod;

private int paymentMethod;

private float amount;

private String hashCode;

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public List<Product> getProducts() {
    return products;
}

public void setProducts(List<Product> products) {
    this.products = products;
}

public BillingInformation getBillingInformation() {
    return billingInformation;
}

public void setBillingInformation(BillingInformation billingInformation) {
    this.billingInformation = billingInformation;
}

public ShippingMethod getShippingMethod() {
    return shippingMethod;
}

public void setShippingMethod(ShippingMethod shippingMethod) {
    this.shippingMethod = shippingMethod;
}

public int getPaymentMethod() {
    return paymentMethod;
}

public void setPaymentMethod(int paymentMethod) {
    this.paymentMethod = paymentMethod;
}

public float getAmount() {
    return amount;
}

public void setAmount(float amount) {
    this.amount = amount;
}

public String getHashCode() {
    return hashCode;
}

public void setHashCode(String hashCode) {
    this.hashCode = hashCode;
}

public static final Parcelable.Creator<CheckoutRequest> CREATOR = new Parcelable.Creator<CheckoutRequest>() {
    public CheckoutRequest createFromParcel(Parcel in) {
        return new CheckoutRequest(in);
    }

    public CheckoutRequest[] newArray(int size) {
        return new CheckoutRequest[size];
    }
};

private CheckoutRequest(Parcel in) {

    email = in.readString();
    products = new ArrayList<Product>();
    in.readTypedList(products, Product.CREATOR);
    shippingMethod = in.readParcelable(ShippingMethod.class.getClassLoader());
    billingInformation = in.readParcelable(BillingInformation.class.getClassLoader());
    paymentMethod = in.readInt();
    amount = in.readFloat();
    hashCode = in.readString();

}

@Override
public int describeContents() {
    return hashCode();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(email);
    dest.writeList(products);
    dest.writeParcelable(shippingMethod, flags);
    dest.writeParcelable(billingInformation, flags);
    dest.writeInt(paymentMethod);
    dest.writeFloat(amount);
    dest.writeString(hashCode);

}

}

1 个答案:

答案 0 :(得分:0)

您使用readTypedList

  

读入包含特定对象类型的给定List项   在当前使用writeTypedList(List)编写的   dataPosition()。该列表必须先前已经写过   writeTypedList(List)具有相同的对象类型。

所以你也必须使用writeTypedList,而不是:

dest.writeList(products);