首页 > 解决方案 > skuDetailsList 返回 null

问题描述

我正在尝试使用最新的Google Play 计费库 (2.0.1)实现应用内购买

• 在Internal App Testing 中发布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 Console 上上传了应用程序(内部测试),但它也没有从那里工作

任何帮助表示赞赏...

编辑: 经过大量实验后,我发现计费服务正在断开连接,但原因不如下 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 被调用

标签: javaandroidin-app-billingplay-billing-library

解决方案


问题通过将我的应用程序移至 alpha 测试解决了它


推荐阅读