Android应用程序内订阅:即使购买存在,库存调用也会将购买返回为null

时间:2014-03-05 16:52:25

标签: android in-app-billing google-play-services android-billing

这个问题非常类似Purchased subscription not returned in inventory& Android billing - error you own this item但是他们提供的答案并没有解决我的问题。

我有来自https://code.google.com/p/marketbilling/的最新代码。我签名的apk已安装在设备上,并作为草稿上传到Play商店。我的订阅购买存在于Google电子钱包商户中心,并显示在queryPurchases和queryInventoryAsync中,但在我的PurchaseActivity中显示为null。

此代码来自IabHelper.java中的queryPurchases方法,并显示购买信息:

        for (int i = 0; i < purchaseDataList.size(); ++i) {
            String purchaseData = purchaseDataList.get(i);
            String signature = signatureList.get(i);
            String sku = ownedSkus.get(i);

            logDebug("Purchase Data: "+purchaseData);

            if (Security.verifyPurchase(mSignatureBase64, purchaseData, signature)) {
                logDebug("Sku is owned: " + sku);
                Purchase purchase = new Purchase(itemType, purchaseData, signature);

                logDebug("Purchase Data: "+purchase);

                if (TextUtils.isEmpty(purchase.getToken())) {
                    logWarn("BUG: empty/null token!");
                    logDebug("Purchase data: " + purchaseData);
                }

                // Record ownership and token
                inv.addPurchase(purchase);

                logDebug("Get Purchase Data: "+ inv.getPurchase(sku));
            }
            else {
                logWarn("Purchase signature verification **FAILED**. Not adding item.");
                logDebug("   Purchase data: " + purchaseData);
                logDebug("   Signature: " + signature);
                verificationFailed = true;
            }
        }

此方法“queryInventoryAsync”来自IabHelper.java类并返回库存信息:

public void queryInventoryAsync(final boolean querySkuDetails,
                           final List<String> moreSkus,
                           final QueryInventoryFinishedListener listener) {
    final Handler handler = new Handler();
    checkNotDisposed();
    checkSetupDone("queryInventory");
    flagStartAsync("refresh inventory");
    (new Thread(new Runnable() {
        public void run() {
            IabResult result = new IabResult(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreSkus);
            }
            catch (IabException ex) {
                result = ex.getResult();
            }

            flagEndAsync();

            final IabResult result_f = result;
            final Inventory inv_f = inv;
            if (!mDisposed && listener != null) {
                handler.post(new Runnable() {
                    public void run() {
                        listener.onQueryInventoryFinished(result_f, inv_f);
                        logDebug("Result: "+ result_f.getResponse() + " inventory: "+ inv_f.getAllOwnedSkus().get(0));
                    }
                });
            }
        }
    })).start();
}

这来自我的PurchaseActivity:

IabHelper.QueryInventoryFinishedListener mInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {

        // Is it a failure?
        if (result.isFailure()) {
            Log.i(TAG, "Failed to query inventory: " + result);
            return;
        }else{
            //This returns false
            Log.i(TAG, "Query inventory was successful: " + inventory.hasPurchase(sku)); 
        }

        // Do we have the premium upgrade?
        //This returns null
        Purchase premiumPurchase = inventory.getPurchase(sku);

虽然queryPurchases和queryInventoryAsync显示我的设备和电子邮件帐户存在购买,但getPurchase(sku)显示Purchase为null且hasPurchase(sku)返回false。

有没有人知道为什么会这样?谢谢!

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题并以这种方式解决了它

  • 使用某些版本代码将草稿应用程序上传为alpha或beta。
  • 使用有效订阅的帐户登录设备。
  • 在此设备上安装已签名的应用程序,版本与alpha / beta版本相同。