RandeeCao / marketbilling

Automatically exported from code.google.com/p/marketbilling
1 stars 0 forks source link

IabHelper.querySkuDetails() errors when number of SKUs > 20 #136

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
mService.getSkuDetails() has a 20 SKU limit.  Here is my fix:

    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));
        if (moreSkus != null) skuList.addAll(moreSkus);

        if (skuList.size() == 0) {
            logDebug("queryPrices: nothing to do because there are no SKUs.");
            return BILLING_RESPONSE_RESULT_OK;
        }

        Bundle querySkus = new Bundle();
        ArrayList<String> responseList = new ArrayList<String>();
        // getSkuDetails() only handles 20 at a time.
        for (int i=0; i<skuList.size(); i+=20) {
            ArrayList<String> twentyMaxSkuList = new ArrayList<String>();
            twentyMaxSkuList.addAll( skuList.subList( i, i+20 > skuList.size() ? skuList.size() : i+20 ) );
            querySkus.putStringArrayList(GET_SKU_DETAILS_ITEM_LIST, twentyMaxSkuList);
            Bundle skuDetails = mService.getSkuDetails(3, mContext.getPackageName(),
                    itemType, querySkus);

            if (!skuDetails.containsKey(RESPONSE_GET_SKU_DETAILS_LIST)) {
                int response = getResponseCodeFromBundle(skuDetails);
                if (response != BILLING_RESPONSE_RESULT_OK) {
                    logDebug("getSkuDetails() failed: " + getResponseDesc(response));
                    return response;
                }
                else {
                    logError("getSkuDetails() returned a bundle with neither an error nor a detail list.");
                    return IABHELPER_BAD_RESPONSE;
                }
            }

            responseList.addAll(skuDetails.getStringArrayList(RESPONSE_GET_SKU_DETAILS_LIST));
        }

        for (String thisResponse : responseList) {
            SkuDetails d = new SkuDetails(itemType, thisResponse);
            logDebug("Got sku details: " + d);
            inv.addSkuDetails(d);
        }
        return BILLING_RESPONSE_RESULT_OK;
    }

Original issue reported on code.google.com by david.ma...@einsight.co.za on 27 Jul 2013 at 12:25

GoogleCodeExporter commented 8 years ago
Wow, I was stumped by this issue and surprised not many have seen or reported 
this.
Thanks for your fix!

Original comment by pereiraa...@gmail.com on 15 Aug 2013 at 2:15