skuDetailsList 返回 null

skuDetailsList returning null

我正在尝试使用最新的 Google Play Billing 库 (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 管理中心上传了应用(内部测试),但它也无法正常工作

感谢任何帮助...

编辑: 经过大量实验,我发现 Billing Service 正在断开连接 但原因并非如下 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

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

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

重试连接

@Override
public void onBillingSetupFinished(BillingResult billingResult) {

  if(!billingResult.getResponseCode==BillingClient.BillingResponseCode.OK) {
       mBillingClient.startConnection(this);
  }

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

}

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

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

public void querySKUDetails(SkuDetailsResponseListener listener) {
    if (!isBillingClientReady) {
        return;
    }
    List<String> skuList = new ArrayList<> ();

    // you need to add your new subscription sku here
    skuList.add(BillingConstants.SKU_POPULAR_MONTHLY);
    skuList.add(BillingConstants.SKU_PREMIUM_YEARLY);

    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
    params.setSkusList(skuList).setType(SkuType.SUBS);
    mBillingClient.querySkuDetailsAsync(params.build(),
            listener);
}