Search code examples
androidin-app-purchasein-app-billing

Cannot get SkuDetail using com.android.billingclient:billing


I'm developing a game for Google Play.And it's time for in-app-purchase, I follow the doc, but I cannot get SkuDetail from code.

I use the library com.android.billingclient:billing:1.0.

public void queryProductList(String product) {
    List<String> skuList = new ArrayList<>();
    skuList.add(product);

    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
    params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
    billingClient.querySkuDetailsAsync(params.build(),
            new SkuDetailsResponseListener() {
                @Override
                public void onSkuDetailsResponse(int billingResult,
                                                 List<SkuDetails> skuDetailsList) {

                    if (billingResult == 0) {

                        Log.i(TAG, "getResponseCode ok");

                        if (skuDetailsList == null) {
                            Log.i(TAG, "skuDetailsList null");
                        } else {
                            Log.i(TAG, "skuDetailsList not null:" + skuDetailsList.size());// size is always 0
                        }

                    }

                }
            });
}

Solution

  • This is my project code for fatching SkuDetail. You can some edit and use.

     public void startServiceForConnectInBilling(String billing_period_of_product_1) {
                this.product1 = billing_period_of_product_1;
        
                myBillingClient.startConnection(new BillingClientStateListener() {
                    @Override
                    public void onBillingSetupFinished(BillingResult billingResult) {
                        try {
                            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                                isServiceConnected = true;
                                if (areSubscriptionsSupported()) {
                                    ArrayList<String> arrayList = new ArrayList<>();
                                    arrayList.add(billing_period_of_product_1);
                                    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                                    params.setSkusList(arrayList).setType(BillingClient.SkuType.SUBS);
                                    Runnable queryRequest = () -> myBillingClient.querySkuDetailsAsync(params.build(), (billingResult1, skuDetailsList) -> {
                                        if (billingResult1.getResponseCode() != BillingClient.BillingResponseCode.OK) {
                                            if (BuildConfig.DEBUG)
                                                Log.e(TAG, "Error billing:" + billingResult1.getDebugMessage());
                                        } else if (skuDetailsList != null && skuDetailsList.size() > 0) {
                                            skuResultList.addAll(skuDetailsList);
                                            if (SharedPreferenceHelper.getSharedPreferenceBoolean(context, AppConstants.GOOGLE_ON_OFF, false))
                                                checkCondition();
                                        }
                                    });
                                    executeServiceRequest(queryRequest);
                                }
                            }
                        } catch (Exception e) {
                            e.getMessage();
                        }
                    }
        
                    @Override
                    public void onBillingServiceDisconnected() {
        
                    }
                });
            }
        
            private void executeServiceRequest(Runnable runnable) {
                if (isServiceConnected) {
                    runnable.run();
                } else {
                    // If billing service was disconnected, we try to reconnect 1 time.
                    // (feel free to introduce your retry policy here).
                    startServiceForConnectInBilling(product1);
                }
            }
          private boolean areSubscriptionsSupported() {
                if (myBillingClient == null) {
                    if (BuildConfig.DEBUG)
                        Log.e(TAG, "Billing client was null and quitting");
                    return false;
                }
                BillingResult responseCode = myBillingClient.isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS);
                if (responseCode.getResponseCode() != BillingClient.BillingResponseCode.OK) {
                    if (BuildConfig.DEBUG)
                        Log.e(TAG,
                                "areSubscriptionsSupported() got an error response: " + responseCode);
                }
                return responseCode.getResponseCode() == BillingClient.BillingResponseCode.OK;
            }