在ListView在应用程序内结算的工作流程 [英] In-app billing workflow in a ListView

查看:241
本文介绍了在ListView在应用程序内结算的工作流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了应用内结算的Andr​​oid应用程序,有像6款产品他们就像硬币的用户才能在我的应用程序购买的物品购买。 设置与测试在应用程序完美的作品我读了所有谷歌文档,做他们说什么,但我的问题是,我的产品显示在ListView我调用的函数 mHelper.launchPurchaseFlow 根据在列表中的活动地位,但项目总是消耗或在这里拥有的是我的code:

  IabHelper.QueryInventoryFinishedListener mGotInventoryListener =新IabHelper.QueryInventoryFinishedListener(){
    公共无效onQueryInventoryFinished(IabResult结果,库存盘点){
        Log.d(TAG,查询库存结束了。);

        //我们有没有在此期间被处置的?如果是这样,退出。
        如果(mHelper == NULL)回报;

        //它是一个失败吗?
        如果(result.isFailure()){
            抱怨(无法查询盘点:+结果);
            返回;
        }

        Log.d(TAG,查询库存是成功的。);

        / *
         *检查我们自己的项目。请注意,每次购买,我们检查
         *开发的有效载荷,看它是否是正确的!看到
         * verifyDeveloperPayload()。
         * /

        //我们是否有100个硬币升级?
        购买hundrendCoin = inventory.getPurchase(SKU_hundred);
        如果(hundrendCoin = NULL和放大器;!&安培; verifyDeveloperPayload(hundrendCoin));
        {
            Log.d(TAG,用户拥有它);
            mHelper.consumeAsync(inventory.getPurchase(SKU_hundred),mConsumeFinishedListener);
        }

        //我们是否有225金币升级?
        购买two_hundred_twenty_fiveCoin = inventory.getPurchase(SKU_two_hundred_twenty_five);
        如果(two_hundred_twenty_fiveCoin = NULL和放大器;!&安培; verifyDeveloperPayload(two_hundred_twenty_fiveCoin));
        {
            Log.d(TAG,用户拥有它);
            mHelper.consumeAsync(inventory.getPurchase(SKU_two_hundred_twenty_five),mConsumeFinishedListener);
        }

        //我们有350个金币升级?
        购买three_hundred_fiftyCoin = inventory.getPurchase(SKU_three_hundred_fifty);
        如果(three_hundred_fiftyCoin = NULL和放大器;!&安培; verifyDeveloperPayload(three_hundred_fiftyCoin));
        {
            Log.d(TAG,用户拥有它);
            mHelper.consumeAsync(inventory.getPurchase(SKU_three_hundred_fifty),mConsumeFinishedListener);
        }

        //我们有475金币升级?
        购买four_hundred_seventy_fiveCoin = inventory.getPurchase(SKU_four_hundred_seventy_five);
        如果(four_hundred_seventy_fiveCoin = NULL和放大器;!&安培; verifyDeveloperPayload(four_hundred_seventy_fiveCoin));
        {
            Log.d(TAG,用户拥有它);
            mHelper.consumeAsync(inventory.getPurchase(SKU_four_hundred_seventy_five),mConsumeFinishedListener);
        }

        //我们是否有600金币升级?
        购买six_hundredCoin = inventory.getPurchase(SKU_six_hundred);
        如果(six_hundredCoin = NULL和放大器;!&安培; verifyDeveloperPayload(six_hundredCoin));
        {
            Log.d(TAG,用户拥有它);
            mHelper.consumeAsync(inventory.getPurchase(SKU_six_hundred),mConsumeFinishedListener);
        }

        //我们是否有1225金币升级?
        购买one_thousand_two_hundred_twenty_fiveCoin = inventory.getPurchase(SKU_one_thousand_two_hundred_twenty_five);
        如果(one_thousand_two_hundred_twenty_fiveCoin = NULL和放大器;!&安培; verifyDeveloperPayload(one_thousand_two_hundred_twenty_fiveCoin));
        {
            Log.d(TAG,用户拥有它);
            mHelper.consumeAsync(inventory.getPurchase(SKU_one_thousand_two_hundred_twenty_five),mConsumeFinishedListener);
        }

        mHelper.flagEndAsync();

        Log.d(TAG,初始库存查询完毕;使主界面。);
    }
};
list.setOnItemClickListener(新AdapterView.OnItemClickListener(){

                @覆盖
                公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){

                    BankClass currentItem = BankList.get(位置);
                    CoinItemID = currentItem.itemID;
                    如果(currentItem.quantity == 100)
                    {
                        CoinItemID = currentItem.itemID;
                        串有效负载=;
                        mHelper.launchPurchaseFlow(BankActivity.this,SKU_hundred,RC_REQUEST,
                                mPurchaseFinishedListener,有效载荷);

                    }否则,如果(currentItem.quantity == 225)
                    {
                        CoinItemID = currentItem.itemID;
                        串有效负载=;
                        mHelper.launchPurchaseFlow(BankActivity.this,SKU_two_hundred_twenty_five,RC_REQUEST,
                                mPurchaseFinishedListener,有效载荷);

                    }否则,如果(currentItem.quantity == 350){
                        CoinItemID = currentItem.itemID;
                        串有效负载=;
                        mHelper.launchPurchaseFlow(BankActivity.this,SKU_three_hundred_fifty,RC_REQUEST,
                                mPurchaseFinishedListener,有效载荷);
                    }否则,如果(currentItem.quantity == 475){
                        CoinItemID = currentItem.itemID;
                        串有效负载=;
                        mHelper.launchPurchaseFlow(BankActivity.this,SKU_four_hundred_seventy_five,RC_REQUEST,
                                mPurchaseFinishedListener,有效载荷);

                    }否则,如果(currentItem.quantity == 600){
                        CoinItemID = currentItem.itemID;
                        串有效负载=;
                        mHelper.launchPurchaseFlow(BankActivity.this,SKU_six_hundred,RC_REQUEST,
                                mPurchaseFinishedListener,有效载荷);

                    }否则,如果(currentItem.quantity == 1225){
                        CoinItemID = currentItem.itemID;
                        串有效负载=;
                        mHelper.launchPurchaseFlow(BankActivity.this,SKU_one_thousand_two_hundred_twenty_five,RC_REQUEST,
                                mPurchaseFinishedListener,有效载荷);

                    }
                }
            });
     @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据)
{
    Log.d(TAG,onActivityResult(+请求code +,+结果code +,+数据);
    如果(mHelper == NULL)回报;

    //传递活动结果的帮手处理
    如果(!mHelper.handleActivityResult(要求code,因此code,数据))
    {
        //没有处理,因此处理它自己(在这里就是你会
        //执行不相关的活动结果的任何处理在应用程序
        //结算...
        super.onActivityResult(要求code,因此code,数据);
    }
    其他
    {
        Log.d(TAG,onActivityResult由IABUtil处理。);
    }
}
布尔verifyDeveloperPayload(采购P)
{
    串有效负载= p.getDeveloperPayload();

    / *
     * TODO:验证购买开发商的有效载荷是正确的。这将是
     *您启动购买时发出的相同。
     *
     *警告:开始购买时,本地生成一个随机字符串,
     *确认在这里似乎是一个很好的方法,但是这会在失败
     *的情况下的用户购买一个项目在一个设备上,然后使用在你的应用程序
     *一个不同的设备,因为其他设备上,将不会有访问
     *您最初生成的随机字符串。
     *
     *因此,一个优秀的开发人员有效载荷具有以下特性:
     *
     * 1。如果两个不同的用户购买物品时,有效载荷为他们之间的不同,
     *,使得一个用户的购买不能重播给其他用户。
     *
     * 2.载荷必须使得可以甚至验证它时该应用不是
     *一个谁发起的购买流程(这样的项目购买的用户
     *由用户拥有的其它设备),一台设备的工作。
     *
     *使用自己的服务器来存储和验证整个应用开发者有效载荷
     *安装建议。
     * /

    返回true;
}

//回调时,购买完成后进行
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener =新IabHelper.OnIabPurchaseFinishedListener()
{
    公共无效onIabPurchaseFinished(IabResult结果,购购)
    {
        Log.d(TAG,购买完成:+结果+,购买+购买);

        //如果我们在这期间处置,退出。
        如果(mHelper == NULL)回报;

        如果(result.isFailure())
        {
            抱怨(错误采购:+结果);
            返回;
        }
        如果(!verifyDeveloperPayload(购买)){
            抱怨(错误采购真实性验证失败。);
            返回;
        }

        Log.d(TAG,购买成功);

        如果(purchase.getSku()。等于(SKU_hundred)){
            //买了1/4罐气。所以使用它。
            Log.d(TAG,购买是100个硬币开始消费。);
            mHelper.consumeAsync(购买,mConsumeFinishedListener);

        }
        否则,如果(purchase.getSku()。等于(SKU_two_hundred_twenty_five)){
            Log.d(TAG,购买是225金币启动消费。);
            mHelper.consumeAsync(购买,mConsumeFinishedListener);

        }
        否则,如果(purchase.getSku()。等于(SKU_three_hundred_fifty)){
            Log.d(TAG,购买是350金币启动消费。);
            mHelper.consumeAsync(购买,mConsumeFinishedListener);

        }
        否则,如果(purchase.getSku()。等于(SKU_four_hundred_seventy_five)){
            Log.d(TAG,购买是475金币启动消费。);
            mHelper.consumeAsync(购买,mConsumeFinishedListener);

        }
        否则,如果(purchase.getSku()。等于(SKU_six_hundred)){
            Log.d(TAG,购买是600金币启动消费。);
            mHelper.consumeAsync(购买,mConsumeFinishedListener);

        }
        否则,如果(purchase.getSku()。等于(SKU_one_thousand_two_hundred_twenty_five)){
            Log.d(TAG,购买是1225金币开始消费。);
            mHelper.consumeAsync(购买,mConsumeFinishedListener);

        }

    }
};

//调用时消耗完毕
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =新IabHelper.OnConsumeFinishedListener(){
    公共无效onConsumeFinished(购购,IabResult结果){
        Log.d(TAG,消费完成购买。+ +购买,结果是:+结果);

        //如果我们在这期间处置,退出。
        如果(mHelper == NULL)回报;

        //我们知道这是气的SKU,因为它是我们的消费只有一个,
        //所以我们不检查其SKU被消耗。如果你有一个以上的
        // SKU,你应该检查...
        如果(result.isSuccess())
        {
            //成功使用,所以我们应用在该项目的影响我们
            //游戏世界的逻辑,这对我们来说意味着填补了煤气罐有点
            (新BuyCoinsTask())执行()。
            Log.d(TAG,消费成功的部署。);
        }
        其他
        {
            抱怨(错误而功耗:+结果);
        }

        Log.d(TAG,最终消费流。);
    }
};
 

任何帮助将是AP preciated。

银行类:

 公共类BankClass
{
  公众诠释ITEMID;
  公众诠释数量;
  公共字符串的价格;
  公共BankClass(INT ITEMID,诠释数量,串价格)
  {
      this.itemID = ITEMID;
      this.quantity =数量;
      this.price =价格;
  }
}
 

下面是我做多消费。

 名单,其中,购买>购买=新的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 =新IabHelper.OnConsumeMultiFinishedListener()
        {

            @覆盖
            公共无效onConsumeMultiFinished(名单<购买>采购,清单和LT; IabResult>结果)
            {
                Log.d(TAG,消费完成购买。+ +购买,结果是:+结果);

                //如果我们在这期间处置,退出。
                如果(mHelper == NULL)回报;

                //我们知道这是气的SKU,因为它是我们的消费只有一个,
                //所以我们不检查其SKU被消耗。如果你有一个以上的
                // SKU,你应该检查...
                的for(int i = 0; I< results.size();我++)
                {
                    如果(results.get(ⅰ).isSuccess())
                    {
                        //成功使用,所以我们应用在该项目的影响我们
                        //游戏世界的逻辑,这对我们来说意味着填补了煤气罐有点

                        Log.d(TAG,消费成功的部署。);
                    } 其他 {
                        抱怨(错误而功耗:+结果);
                    }
                }
                Log.d(TAG,最终消费流。);
            }

        };
        mHelper.consumeAsync(采购,onCusumeListner);
 

但结果我不知道如何根据位置有处理结果所有项目的成功,我应该跑一个for循环,或有另一种方式,但它仍然在各方面发生崩溃。

logcat的输出:

  04-17 13:01:31.083 8312-8964 / net.httpiamheroic.herioc E / AndroidRuntime:致命异常:螺纹3333
  工艺:net.httpiamheroic.herioc,PID:8312
显示java.lang.NullPointerException:尝试从外地java.lang.String中net.httpiamheroic.herioc.net.httpiamherioc.util.Purchase.mItemType读一空对象引用
        在net.httpiamheroic.herioc.net.httpiamherioc.util.IabHelper.consume(IabHelper.java:660)
        在net.httpiamheroic.herioc.net.httpiamherioc.util.IabHelper $ 3.run(IabHelper.java:953)
        在java.lang.Thread.run(Thread.java:818)
 

解决方案

既然你就吃下多个项目,建议通过传递您的采购列表中使用consumeAsync多个项目的方法。你可以找到它里面 TrivialDrive 为Iab的示例应用程序版本3

  / **
 *同{@link consumeAsync},但在一次多个项目。
 *参数购买PurchaseInfo对象的列表重新presenting消费的购买。
 *参数监听器当消费操作完成通知的监听器。
 * /
公共无效consumeAsync(名单<购买>购买,OnConsumeMultiFinishedListener监听器){
 

使用onConsumeMultiFinished为你的回调

 公共无效onConsumeMultiFinished(名单<购买>采购,清单和LT; IabResult>结果);
 

而应采取适当的发送异步照顾消耗的请求。更新你code和售后的任何问题。

I implemented in-app billing in Android application, have like 6 product they are like coins the user will buy in order to buy items in my app. The setup and the testing for in-app works perfectly I read all google documents and did what they said but my problem is that my product are shown in a listView i called the function mHelper.launchPurchaseFlow according to the position in the list in the activity but the items are always consumed or owned here is my code:

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.");
    }
};

Any help would be appreciated.

Bank class:

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;
  }
}

Here is what i did with multi consuming .

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);

but in the result i don't know how to handle the success of the result for all items according to there position should i run a for loop for it or there is another way but still its crashing in all ways.

Logcat output:

 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)

解决方案

Since you're consuming multiple items, would suggest to use consumeAsync for multiple items method by passing your list of purchases. You can find it inside TrivialDrive sample app for Iab ver 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) {

Use onConsumeMultiFinished as your callback

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

It should take care of sending proper async consume requests. Update you code and post any problems.

这篇关于在ListView在应用程序内结算的工作流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆