java - 无法购买商品,错误响应 : 7:Item Already Owned

标签 java android in-app-purchase in-app-billing

当我尝试再次购买商品时,出现以下错误。请不要将其作为重复项关闭。我知道堆栈溢出中有很多这样的问题,但似乎没有一个有帮助。请引用我的代码。

 public class BtnListener implements View.OnClickListener
{
    // On-click event handler for all the buttons
    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {

            case R.id.TwoSeconds:
                mHelper.launchPurchaseFlow(TimeBoosterActivity.this, ITEM_SKU, 10001,
                        mPurchaseFinishedListener, "2");
                break;
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data)
{
    if (!mHelper.handleActivityResult(requestCode,
            resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
        = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result,
                                      Purchase purchase)
    {
        if (result.isFailure()) {
            // Handle error
            return;
        }
        else if (purchase.getSku().equals(ITEM_SKU)) {
            consumeItem();
            //buyButton.setEnabled(false);

        }

    }
};

public void consumeItem() {
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
}

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

        if (result.isFailure()) {
            Toast.makeText(getApplicationContext(), "Failed to consume item",
                    Toast.LENGTH_SHORT).show();
        } else {
            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                    mConsumeFinishedListener);
        }
    }
};

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
        new IabHelper.OnConsumeFinishedListener() {
            public void onConsumeFinished(Purchase purchase,
                                          IabResult result) {

                if (result.isSuccess()) {
                    String getSeconds=purchase.getDeveloperPayload();
                    SharedPreferences saveTwoSeconds=getSharedPreferences(getSeconds, Context.MODE_PRIVATE);
                    //clickButton.setEnabled(true);
                } else {
                    // handle error
                    Toast.makeText(getApplicationContext(), "Failed to consume item",
                            Toast.LENGTH_SHORT).show();
                }
            }
        };

@Override
public void onDestroy() {
    super.onDestroy();
    if (mHelper != null) mHelper.dispose();
    mHelper = null;
}

最佳答案

我认为由于 onActivityResult 中的代码,您的消费购买未被调用。

尝试使用下面的代码

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                            Intent data){
  super.onActivityResult(requestCode, resultCode, data);
 if (requestCode == <Code you sent during launching purchase flow> || requestCode == <Code you sent during launching consume flow> )
     mHelper.handleActivityResult(requestCode,resultCode, data))
   }
}

关于java - 无法购买商品,错误响应 : 7:Item Already Owned,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35680576/

相关文章:

java - Eclipse 2.0.2 和 Maven 0.17 配置

java - 无法从 Java Netbeans 连接到 Mariadb

Android - 膨胀 TextView 时出错

iphone - 应用内购买 (IAP) 简单问题(我有问题)

java - 尝试使用冒泡排序对随机整数数组从最大到最小进行排序

java - 在Hadoop上以Java代码运行exe文件

android - 如何强制 Android 调试的内存压力?

android - 在 Android 中使用 Canvas 转换实现 'rubberbanding' 相机

Android 应用内计费错误 : Illegal state for operation (launchPurchaseFlow): IAB helper is not set up

Android 应用内购买免费试用订阅 - 如何避免滥用?