当前位置: 首页 > 知识库问答 >
问题:

ListView中的应用内计费工作流

冯阳华
2023-03-14

我在Android应用程序中实现了应用内计费,有6款产品,它们就像用户为了在我的应用中购买物品而购买的硬币。应用程序内的设置和测试非常完美,我阅读了所有谷歌文档,并按照他们说的做了,但我的问题是,我的产品显示在一个我称为mHelper函数的列表视图中。根据活动列表中的位置启动PurchaseFlow,但物品总是被消费或拥有。以下是我的代码:

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        Log.d(TAG, "Query inventory finished.");

        // Have we been disposed of in the meantime? If so, quit.
        if (mHelper == null) return;

        // Is it a failure?
        if (result.isFailure()) {
            complain("Failed to query inventory: " + result);
            return;
        }

        Log.d(TAG, "Query inventory was successful.");

        /*
         * Check for items we own. Notice that for each purchase, we check
         * the developer payload to see if it's correct! See
         * verifyDeveloperPayload().
         */

        // Do we have the 100 coins upgrade?
        Purchase hundrendCoin = inventory.getPurchase(SKU_hundred);
        if(hundrendCoin != null && verifyDeveloperPayload(hundrendCoin));
        {
            Log.d(TAG, "User have it ");
            mHelper.consumeAsync(inventory.getPurchase(SKU_hundred), mConsumeFinishedListener);
        }

        // Do we have the 225 coins upgrade?
        Purchase two_hundred_twenty_fiveCoin = inventory.getPurchase(SKU_two_hundred_twenty_five);
        if(two_hundred_twenty_fiveCoin != null && verifyDeveloperPayload(two_hundred_twenty_fiveCoin));
        {
            Log.d(TAG, "User have it ");
            mHelper.consumeAsync(inventory.getPurchase(SKU_two_hundred_twenty_five), mConsumeFinishedListener);
        }

        // Do we have the 350 coins upgrade?
        Purchase three_hundred_fiftyCoin = inventory.getPurchase(SKU_three_hundred_fifty);
        if(three_hundred_fiftyCoin != null && verifyDeveloperPayload(three_hundred_fiftyCoin));
        {
            Log.d(TAG, "User have it ");
            mHelper.consumeAsync(inventory.getPurchase(SKU_three_hundred_fifty), mConsumeFinishedListener);
        }

        // Do we have the 475 coins upgrade?
        Purchase four_hundred_seventy_fiveCoin = inventory.getPurchase(SKU_four_hundred_seventy_five);
        if(four_hundred_seventy_fiveCoin != null && verifyDeveloperPayload(four_hundred_seventy_fiveCoin));
        {
            Log.d(TAG, "User have it ");
            mHelper.consumeAsync(inventory.getPurchase(SKU_four_hundred_seventy_five), mConsumeFinishedListener);
        }

        // Do we have the 600 coins upgrade?
        Purchase six_hundredCoin = inventory.getPurchase(SKU_six_hundred);
        if(six_hundredCoin != null && verifyDeveloperPayload(six_hundredCoin));
        {
            Log.d(TAG, "User have it");
            mHelper.consumeAsync(inventory.getPurchase(SKU_six_hundred), mConsumeFinishedListener);
        }

        // Do we have the 1225 coins upgrade?
        Purchase one_thousand_two_hundred_twenty_fiveCoin = inventory.getPurchase(SKU_one_thousand_two_hundred_twenty_five);
        if(one_thousand_two_hundred_twenty_fiveCoin != null && verifyDeveloperPayload(one_thousand_two_hundred_twenty_fiveCoin));
        {
            Log.d(TAG, "User have it ");
            mHelper.consumeAsync(inventory.getPurchase(SKU_one_thousand_two_hundred_twenty_five), mConsumeFinishedListener);
        }

        mHelper.flagEndAsync();

        Log.d(TAG, "Initial inventory query finished; enabling main UI.");
    }
};
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    BankClass currentItem = BankList.get(position);
                    CoinItemID = currentItem.itemID;
                    if (currentItem.quantity == 100)
                    {
                        CoinItemID = currentItem.itemID;
                        String payload = "";
                        mHelper.launchPurchaseFlow(BankActivity.this, SKU_hundred, RC_REQUEST,
                                mPurchaseFinishedListener, payload);

                    } else if (currentItem.quantity == 225)
                    {
                        CoinItemID = currentItem.itemID;
                        String payload = "";
                        mHelper.launchPurchaseFlow(BankActivity.this, SKU_two_hundred_twenty_five, RC_REQUEST,
                                mPurchaseFinishedListener, payload);

                    } else if (currentItem.quantity == 350) {
                        CoinItemID = currentItem.itemID;
                        String payload = "";
                        mHelper.launchPurchaseFlow(BankActivity.this, SKU_three_hundred_fifty, RC_REQUEST,
                                mPurchaseFinishedListener, payload);
                    } else if (currentItem.quantity == 475) {
                        CoinItemID = currentItem.itemID;
                        String payload = "";
                        mHelper.launchPurchaseFlow(BankActivity.this, SKU_four_hundred_seventy_five, RC_REQUEST,
                                mPurchaseFinishedListener, payload);

                    } else if (currentItem.quantity == 600) {
                        CoinItemID = currentItem.itemID;
                        String payload = "";
                        mHelper.launchPurchaseFlow(BankActivity.this, SKU_six_hundred, RC_REQUEST,
                                mPurchaseFinishedListener, payload);

                    } else if (currentItem.quantity == 1225) {
                        CoinItemID = currentItem.itemID;
                        String payload = "";
                        mHelper.launchPurchaseFlow(BankActivity.this, SKU_one_thousand_two_hundred_twenty_five, RC_REQUEST,
                                mPurchaseFinishedListener, payload);

                    }
                }
            });
     @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
    if (mHelper == null) return;

    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data))
    {
        // not handled, so handle it ourselves (here's where you'd
        // perform any handling of activity results not related to in-app
        // billing...
        super.onActivityResult(requestCode, resultCode, data);
    }
    else
    {
        Log.d(TAG, "onActivityResult handled by IABUtil.");
    }
}
boolean verifyDeveloperPayload(Purchase p)
{
    String payload = p.getDeveloperPayload();

    /*
     * TODO: verify that the developer payload of the purchase is correct. It will be
     * the same one that you sent when initiating the purchase.
     *
     * WARNING: Locally generating a random string when starting a purchase and
     * verifying it here might seem like a good approach, but this will fail in the
     * case where the user purchases an item on one device and then uses your app on
     * a different device, because on the other device you will not have access to the
     * random string you originally generated.
     *
     * So a good developer payload has these characteristics:
     *
     * 1. If two different users purchase an item, the payload is different between them,
     *    so that one user's purchase can't be replayed to another user.
     *
     * 2. The payload must be such that you can verify it even when the app wasn't the
     *    one who initiated the purchase flow (so that items purchased by the user on
     *    one device work on other devices owned by the user).
     *
     * Using your own server to store and verify developer payloads across app
     * installations is recommended.
     */

    return true;
}

// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener()
{
    public void onIabPurchaseFinished(IabResult result, Purchase purchase)
    {
        Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);

        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

        if (result.isFailure())
        {
            complain("Error purchasing: " + result);
            return;
        }
        if (!verifyDeveloperPayload(purchase)) {
            complain("Error purchasing. Authenticity verification failed.");
            return;
        }

        Log.d(TAG, "Purchase successful.");

        if (purchase.getSku().equals(SKU_hundred)) {
            // bought 1/4 tank of gas. So consume it.
            Log.d(TAG, "Purchase is 100 Coins. Starting consumption.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);

        }
        else if (purchase.getSku().equals(SKU_two_hundred_twenty_five)) {
            Log.d(TAG, "Purchase is 225 Coins. Starting consumption.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);

        }
        else if (purchase.getSku().equals(SKU_three_hundred_fifty)) {
            Log.d(TAG, "Purchase is 350 Coins. Starting consumption.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);

        }
        else if (purchase.getSku().equals(SKU_four_hundred_seventy_five)) {
            Log.d(TAG, "Purchase is 475 Coins. Starting consumption.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);

        }
        else if (purchase.getSku().equals(SKU_six_hundred)) {
            Log.d(TAG, "Purchase is 600 Coins. Starting consumption.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);

        }
        else if (purchase.getSku().equals(SKU_one_thousand_two_hundred_twenty_five)) {
            Log.d(TAG, "Purchase is 1225 Coins. Starting consumption.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);

        }

    }
};

// Called when consumption is complete
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
    public void onConsumeFinished(Purchase purchase, IabResult result) {
        Log.d(TAG, "Consumption finished. Purchase: " + purchase + ", result: " + result);

        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

        // We know this is the "gas" sku because it's the only one we consume,
        // so we don't check which sku was consumed. If you have more than one
        // sku, you probably should check...
        if (result.isSuccess())
        {
            // successfully consumed, so we apply the effects of the item in our
            // game world's logic, which in our case means filling the gas tank a bit
            (new BuyCoinsTask()).execute();
            Log.d(TAG, "Consumption successful. Provisioning.");
        }
        else
        {
            complain("Error while consuming: " + result);
        }

        Log.d(TAG, "End consumption flow.");
    }
};

任何帮助都将不胜感激。

银行类:

public class BankClass
{
  public int itemID;
  public int quantity;
  public String price;
  public BankClass(int itemID,int quantity,String price)
  {
      this.itemID=itemID;
      this.quantity=quantity;
      this.price=price;
  }
}

以下是我对多重消费所做的。

List<Purchase> purchases = new ArrayList<>();
        purchases.add(inventory.getPurchase(SKU_hundred));
        purchases.add(inventory.getPurchase(SKU_two_hundred_twenty_five));
        purchases.add(inventory.getPurchase(SKU_three_hundred_fifty));
        purchases.add(inventory.getPurchase(SKU_four_hundred_seventy_five));
        purchases.add(inventory.getPurchase(SKU_six_hundred));
        purchases.add(inventory.getPurchase(SKU_one_thousand_two_hundred_twenty_five));

        IabHelper.OnConsumeMultiFinishedListener onCusumeListner = new IabHelper.OnConsumeMultiFinishedListener()
        {

            @Override
            public void onConsumeMultiFinished(List<Purchase> purchases, List<IabResult> results)
            {
                Log.d(TAG, "Consumption finished. Purchase: " + purchases + ", result: " + results);

                // if we were disposed of in the meantime, quit.
                if (mHelper == null) return;

                // We know this is the "gas" sku because it's the only one we consume,
                // so we don't check which sku was consumed. If you have more than one
                // sku, you probably should check...
                for(int i=0;i<results.size();i++)
                {
                    if (results.get(i).isSuccess())
                    {
                        // successfully consumed, so we apply the effects of the item in our
                        // game world's logic, which in our case means filling the gas tank a bit

                        Log.d(TAG, "Consumption successful. Provisioning.");
                    } else {
                        complain("Error while consuming: " + results);
                    }
                }
                Log.d(TAG, "End consumption flow.");
            }

        };
        mHelper.consumeAsync(purchases, onCusumeListner);

但在结果中,我不知道如何根据位置处理所有项目的结果是否成功。我应该为它运行for循环,还是有其他方法,但它仍然会在所有方面崩溃。

Logcat输出:

 04-17 13:01:31.083    8312-8964/net.httpiamheroic.herioc    E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-3333
  Process: net.httpiamheroic.herioc, PID: 8312
java.lang.NullPointerException: Attempt to read from field   'java.lang.String   net.httpiamheroic.herioc.net.httpiamherioc.util.Purchase.mItemType' on a   null object reference
        at   net.httpiamheroic.herioc.net.httpiamherioc.util.IabHelper.consume(IabHelper.java:660)
        at net.httpiamheroic.herioc.net.httpiamherioc.util.IabHelper$3.run(IabHelper.java:953)
        at java.lang.Thread.run(Thread.java:818)

共有2个答案

梁丘威
2023-03-14

把你的课改成这些

public class BankClass {

    public int itemId;
    public int quantity;
    public String Price;

    public BankClass(int _itemId,int _quantity,String _Price){
        itemId = _itemId;
        quantity = _quantity;
        Price = _Price;
    }

    public int getItemId() {
        return itemId;
    }

    public String getPrice() {
        return Price;
    }

    public int getQuantity() {
        return quantity;
    }
}

在listitem中,单击更改这些代码,如bewlo

BankClass currentItem = BankList.get(position);
                    CoinItemID = currentItem.getItemId();
                    if (currentItem.getQuantity() == 100)
                    {
                        CoinItemID = currentItem.getItemId();
                        String payload = "";
                        mHelper.launchPurchaseFlow(BankActivity.this, SKU_hundred, RC_REQUEST,
                                mPurchaseFinishedListener, payload);

                    }
雍光远
2023-03-14

由于您正在消费多个商品,建议通过传递您的购买列表,使用ConsumeAncy for multiple items方法。你可以在Iab 3版的驱动示例应用程序中找到它

/**
 * Same as {@link consumeAsync}, but for multiple items at once.
 * @param purchases The list of PurchaseInfo objects representing the purchases to consume.
 * @param listener The listener to notify when the consumption operation finishes.
 */
public void consumeAsync(List<Purchase> purchases, OnConsumeMultiFinishedListener listener) {

使用onConsumeMultiFinished作为回调函数

public void onConsumeMultiFinished(List<Purchase> purchases, List<IabResult> results);

它应该负责发送正确的异步消费请求。更新你的代码并发布任何问题。

 类似资料:
  • 为了测试Android应用内计费v3,我实现了一个对话框,其中添加了一个选项适配器。我添加了测试SKU,“Android.测试.购买”,“Android.测试.取消”,“Android.测试.退款”,“Android.测试.项目不可用”。 当我使用该对话框启动购买流时,一切正常,我能够购买该商品,并且该商品是绝对购买的。然而,当我的应用程序尝试刷新UI时,我会收到一个nullpointerexce

  • 启动购买流后,onActivityResult方法需要什么? 来自平凡的驱动器示例: msgstr"在这里,您可以对与应用内计费无关的活动结果执行任何处理" 这是否意味着您需要更新用户的清单或显示警报框?如果是这样,我已经在OnConsumeFinishedListener中这样做了。我已经测试了我的代码,留下了上面的onActivityResult方法,看起来没问题。这可能会导致任何问题吗? 或

  • 我正在尝试实现销售消耗品(硬币)的谷歌应用内计费。我用一个非消耗品测试了它,它工作正常。但是我不能让它成为消耗品。每次我测试它,我只能买一次!这是我的代码: 公共类MainActivity扩展了AppCompatActivity{IabHelper mHelper; ### 对不起我的英语,谢谢。

  • 我意识到每次应用程序运行时都没有必要检查谷歌play商店,尤其是如果购买已经准备好了。如果用户在使用应用程序时没有GPRS/WiFi,这也可能会导致问题! 因此,我在考虑创建一个共享偏好,它将作为检查用户是否购买了应用内购买的条件。 还有其他更安全的方法吗?正如我所读到的,共享首选项可以很容易地改变。

  • 我在测试我的应用内计费时遇到了麻烦。 (使用Google Play应用内计费版本3 API) 问题: 已退款的应用程序内购买仍然存在于购买列表中,该列表由BillingClient.query购买()提供。 当我在一月份尝试退款时,退款的物品从购物清单上消失了。 我所做的: 作为测试人员购买了一些物品。 (我很确定对话框说是测试购买。) 之后在谷歌游戏控制台退款。 等到他们的付款状态变成退款。 清

  • 我一直在徒劳地想弄明白这一点。为我的Android应用程序设置应用内账单需要什么。尤其是,整个谷歌钱包商户账户让我感到困惑。 因此,如果我做对了,以下是步骤: 创建Google开发者账户 在与Developer相同的帐户上创建Google钱包商户帐户 上传你的. apk到开发者控制台 添加您希望销售的产品 在你的应用程序中编程这些东西 上传new. apk到控制台,添加测试帐户。 测试应用的应用内