应用内帐单:“查询可购买的商品"返回0项 [英] In-App Billing: "Query items available for purchase" returns 0 items

查看:54
本文介绍了应用内帐单:“查询可购买的商品"返回0项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用实施应用内结算.
我正在跟踪Google的TriviaDrive示例应用程序中使用的实现以及开发者网站上的相关文档.
我的代码按预期工作,但是当我尝试 查询可购买的商品" ,即使我创建了产品,生成的库存"对象也包含0个对象.

I am trying to implement in-app-billing for my app.
I am following the implementation used in google's TriviaDrive sample app, and the relevant documentation on the Developer website.
My code is working as expected but when I try to "Query Items Available for Purchase", the resulting Inventory object contains 0 objects, even though I have created a product.

我已经使用Google Play开发者控制台创建了ID为paid_version的被管理产品,如下图所示:

I have created a Managed Product with the id paid_version using the Google Play Developer Console, as shown in the image below:

文档指出要检索产品详细信息,请在IabHelper实例上调用queryInventoryAsync(boolean, List, QueryInventoryFinishedListener)."

The documentation indicates that "To retrieve the product details, call queryInventoryAsync(boolean, List, QueryInventoryFinishedListener) on your IabHelper instance."

我用自己的代码致电
mHelper.queryInventoryAsync(true, iabItemSkus, mQueryFinishedListener)
其中:
mHelper是我的IabHelper实例
iabItemSkus是一个列表,其中包含一个值为"paid_version"的单项.
mQueryFinishedListener是我在下面定义的侦听器.

In my own code I call
mHelper.queryInventoryAsync(true, iabItemSkus, mQueryFinishedListener)
where:
mHelper is my IabHelper instance
iabItemSkus is a List containing a single item with the value "paid_version"
mQueryFinishedListener is my listener defined below.

IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
        @Override
        public void onQueryInventoryFinished(IabResult result, Inventory inv) {
            if (result.isFailure()) {
                Log.d(TAG, "Querying Inventory Failed: " + result);
                return;
            }

            Log.d(TAG, "Title: " + inv.getSkuDetails(SKU_PAID).getTitle());
            Log.d(TAG, "Description: " + inv.getSkuDetails(SKU_PAID).getDescription());
            Log.d(TAG, "Price = " + inv.getSkuDetails(SKU_PAID).getPrice());
        }
    };

但是在调试时,我可以看到在QueryInventoryFinishedListener中传递回的Inventory对象包含0个项目,因此像inv.getSkuDetails(SKU_PAID).getTitle()这样的调用会给出空指针异常.

But on debugging I can see that the Inventory object passed back in the QueryInventoryFinishedListener contains 0 items, and so calls like inv.getSkuDetails(SKU_PAID).getTitle() give a null pointer exception.

我无法弄清楚哪里出了问题.我期望Inventory对象包含我的paid_version应用内商品的详细信息.

I can't work out where I'm going wrong. I was expecting the Inventory object to contain the details for my paid_version in-app product.

下面只是我的代码和LogCat的部分,我认为与该问题有关(试图避免使代码过载!),但是如果对代码的其他部分进行更多详细的介绍会有所帮助,请告诉我.

Below are just the parts of my code and LogCat I think are relevant to this problem (trying to avoid giving you code overload!), but if more detail on some other part of the code would be helpful, let me know.

根据我的活动:

...
private static final String SKU_PAID = "paid_version";
private static final String TAG = "MyActivity";
private IabHelper mHelper;
...

IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
    @Override
    public void onQueryInventoryFinished(IabResult result, Inventory inv) {
        if (result.isFailure()) {
            Log.d(TAG, "Querying Inventory Failed: " + result);
            return;
        }

        Log.d(TAG, "Title: " + inv.getSkuDetails(SKU_PAID).getTitle()); // <-- Line 266 of MyActivity.java
        Log.d(TAG, "Description: " + inv.getSkuDetails(SKU_PAID).getDescription());
        Log.d(TAG, "Price = " + inv.getSkuDetails(SKU_PAID).getPrice());
    }
};
...

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

    final List<String> iabItemSkus = new ArrayList<String>();
    iabItemSkus.add(SKU_PAID);
    // In App Billing
    String base64EncodedPublicKey = "... My Public Key ...";
    mHelper = new IabHelper(this, base64EncodedPublicKey);
    mHelper.enableDebugLogging(true);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
       @Override
       public void onIabSetupFinished(IabResult result) {
           if (!result.isSuccess()) {
               Log.d(TAG, "Problem setting up In-app Billing: " + result);
           }
           // Have we been disposed of in the meantime? If so, quit.
           if (mHelper == null) return;

           // IAB is fully set up. Now, let's get list of available items
           Log.d(TAG, "Setup successful. Querying inventory.");
           mHelper.queryInventoryAsync(true, iabItemSkus, mQueryFinishedListener);
       }
    });
    ...
}

通过我的LogCat:

...
05-13 19:46:59.609  22390-22390/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Starting in-app billing setup.
05-13 19:46:59.629  22390-22390/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Billing service connected.
05-13 19:46:59.629  22390-22390/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Checking for in-app billing 3 support.
05-13 19:46:59.629  22390-22390/xxx.xxxxxx.xxxxxx D/IabHelper﹕ In-app billing version 3 supported for xxx.xxxxxx.xxxxxx
05-13 19:46:59.639  22390-22390/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Subscriptions AVAILABLE.
05-13 19:46:59.639  22390-22390/xxx.xxxxxx.xxxxxx D/MyActivity﹕ Setup successful. Querying inventory.
05-13 19:46:59.639  22390-22390/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Starting async operation: refresh inventory
05-13 19:46:59.649  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Querying owned items, item type: inapp
05-13 19:46:59.649  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Package name: xxx.xxxxxx.xxxxxx
05-13 19:46:59.649  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Calling getPurchases with continuation token: null
05-13 19:46:59.659  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Owned items response: 0
05-13 19:46:59.659  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Continuation token: null
05-13 19:46:59.659  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Querying SKU details.
05-13 19:46:59.689  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Querying owned items, item type: subs
05-13 19:46:59.689  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Package name: xxx.xxxxxx.xxxxxx
05-13 19:46:59.689  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Calling getPurchases with continuation token: null
05-13 19:46:59.699  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Owned items response: 0
05-13 19:46:59.699  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Continuation token: null
05-13 19:46:59.699  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Querying SKU details.
05-13 19:46:59.829  22390-22596/xxx.xxxxxx.xxxxxx D/IabHelper﹕ Ending async operation: refresh inventory
05-13 19:46:59.829  22390-22390/xxx.xxxxxx.xxxxxx D/AndroidRuntime﹕ Shutting down VM
05-13 19:46:59.829  22390-22390/xxx.xxxxxx.xxxxxx W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41b31ba8)
05-13 19:46:59.839  22390-22390/xxx.xxxxxx.xxxxxx E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: xxx.xxxxxx.xxxxxx, PID: 22390
    java.lang.NullPointerException
            at xxx.xxxxxx.xxxxxx.MyActivity$1.onQueryInventoryFinished(MyActivity.java:266)
            at xxx.xxxxxx.xxxxxx.util.IabHelper$2$1.run(IabHelper.java:630)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
...

PS:我刚刚更新了代码以突出显示第266行.

PS: I've just updated my code to highlight which is line 266.

其他

上传APK 12小时后,我遇到了这个问题,正如您从LogCat中看到的那样,它表示我的应用程序为"In-app billing version 3 supported".
无论我将IN-APP PRODUCT的状态设置为活动还是非活动,问题都仍然存在.

I was experiencing this problem 12 hours after uploading my APK, and as you can see from the LogCat, it indicates "In-app billing version 3 supported" for my app.
The problem persisted regardless of whether I set the Status of the IN-APP PRODUCT as active or inactive.

现在24小时后,它神奇地决定了可以正常工作.

Now 24 hours later it magically decides to work correctly.

由此,我只能确定这是Google Play的问题,而不是我的代码.

From this I can only determine that it was a problem with Google Play, not with my code.

推荐答案

我遇到了同样的问题.如果您查看IabHelper代码,我认为问题是这样的:

I'm having the same problem. If you look at the IabHelper code, I think the issue is this:

内部查询库存:

if (querySkuDetails) {
    r = querySkuDetails(ITEM_TYPE_INAPP, inv, moreItemSkus);
    if (r != BILLING_RESPONSE_RESULT_OK) {
        throw new IabException(r, "Error refreshing inventory (querying prices of items).");
    }
}

但随后是querySkuDetails的前几行:

But then the first few lines of querySkuDetails:

int querySkuDetails(String itemType, Inventory inv, List<String> moreSkus)
        throws RemoteException, JSONException {
    logDebug("Querying SKU details.");
    ArrayList<String> skuList = new ArrayList<String>();
    skuList.addAll(inv.getAllOwnedSkus(itemType));

请注意,最后一行用所有所有 skus填充skuList.并非所有可用的.

Notice the last line populates the skuList with all owned skus. Not all available ones.

因此,答案是IabHelper并不真正支持查询可用的购买.

So the answer is that the IabHelper doesn't really support querying available purchases.

这篇关于应用内帐单:“查询可购买的商品"返回0项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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