skuDetailsList返回null

时间:2019-07-01 08:13:09

标签: java android in-app-billing play-billing-library

我正在尝试使用最新的 Google Play结算库(2.0.1)

实施应用内购买

•在内部应用程序测试中发布apk后,我已将产品ID添加到skuList和Google Play控制台中

•但是,当我启动billingFlow时,我得到以下信息

2019-07-01 13:17:02.436 1225-1225/com.mypackage.myapp D/InAppBilling: Other code5
2019-07-01 13:17:02.436 1225-1225/com.mypackage.myapp D/InAppBilling: Invalid SKU input params. SKU can't be null

这是我的代码:

List skuList = new ArrayList<>();

        skuList.add("product_1");
        skuList.add("product_2");
        skuList.add("product_3");
        skuList.add("product_"4);

 SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
        params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
        mBillingClient.querySkuDetailsAsync(params.build(),
                new SkuDetailsResponseListener() {
                    @Override
                    public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
                        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
                                && skuDetailsList != null) {

                            for (Object skuDetailsObject : skuDetailsList) {
                                skuDetails = (SkuDetails) skuDetailsObject;
                                String sku = skuDetails.getSku();
                                String price = skuDetails.getPrice();
                                if ("product_1".equals(sku)) {

                                    textA.setText(price);
                                } else if ("product_2".equals(sku)) {
                                    textB.setText(price);
                                } else if ("product_3".equals(sku)) {
                                    textC.setText(price);
                                } else if ("product_4".equals(sku)) {
                                    textD.setText(price);
                                }

} else {
                            Log.d(TAG, "Sku is null");
                        }
                        Log.d(TAG, "i got response");
                        Log.d(TAG, String.valueOf(billingResult.getResponseCode()));
                        Log.d(TAG, billingResult.getDebugMessage());


                    }


                });
        mBuyButton = findViewById(R.id.pay);
        mBuyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                    BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                            .setSkuDetails(skuDetails)
                            .build();
          mBillingClient.launchBillingFlow(PayActivity.this, flowParams);
}
        });

这是logcat

2019-07-01 13:17:04.173 1225-1225/com.mypackage.myapp D/InAppBilling: Sku is null
2019-07-01 13:17:04.173 1225-1225/com.mypackage.myapp D/InAppBilling: i got response
2019-07-01 13:17:04.173 1225-1225/com.mypackage.myapp D/InAppBilling: -1 //billing result response code 
2019-07-01 13:17:04.173 1225-1225/com.mypackage.myapp D/InAppBilling: Service connection is disconnected. //debug message

我还尝试了使用保留的测试产品ID android.test.purchase ,但存在相同的错误

我还已经在Play控制台上上传了应用程序(内部测试),但是从那里也无法正常运行

感谢您的帮助...

编辑: 经过大量实验,我发现计费服务已断开连接 但原因不是如下 https://developer.android.com/reference/com/android/billingclient/api/BillingClient.BillingResponse#service_disconnected(-1)

mBillingClient = BillingClient.newBuilder(PayActivity.this).setListener(this).enablePendingPurchases().build();
        mBillingClient.startConnection(new BillingClientStateListener() {


            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {

                Log.d(TAG, "Connection finished");
            }

            @Override
            public void onBillingServiceDisconnected() {
                //TODO implement your own retry policy
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
                mBillingClient.startConnection(this);
            }
        });

并在获得响应后调用onBillingSetupFinished

4 个答案:

答案 0 :(得分:1)

像这样初始化您的skuList,然后添加产品-

List<String> skuList = new ArrayList<> ();
   skuList.add("product_1");
    skuList.add("product_2");
    skuList.add("product_3");
    skuList.add("product_4");

答案 1 :(得分:1)

重试连接

@Override
            public void onBillingSetupFinished(BillingResult billingResult) {
if(!billingResult.getResponseCode==BillingClient.BillingResponseCode.OK){
mBillingClient.startConnection(this);
}
                Log.d(TAG, "Connection finished");
            }

答案 2 :(得分:1)

在我的情况下,我忘了在以下代码中在游戏机上添加新添加的订阅,然后添加了它,问题就消失了。

from multiprocessing.pool import Pool

# Initializer so that the processes load the catalog only once
def initializer():
    global catalog
    catalog = make_catalog()


with Pool(16, initializer) as pool:
    results = pool.imap_unordered(generate_one_mix, [10]*100_000)

答案 3 :(得分:0)

问题通过将我的应用进行Alpha测试解决了