Querying for Items Available for Purchase
In your application, you can query the item details from Google Play using the In-app Billing Version 3 API. To pass a request to the In-app Billing service, first create a Bundle
that contains a String ArrayList
of product IDs with key "ITEM_ID_LIST", where each string is a product ID for an purchasable item.
ArrayList skuList = new ArrayList(); skuList.add("premiumUpgrade"); skuList.add("gas"); Bundle querySkus = new Bundle(); querySkus.putStringArrayList(“ITEM_ID_LIST”, skuList);
To retrieve this information from Google Play, call the
getSkuDetails
method on the In-app Billing Version 3 API, and pass the method the In-app Billing API version (“3”), the package name of your calling app, the purchase type (“inapp”), and theBundle
that you created.Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);If the request is successful, the returned
Bundle
has a response code ofBILLING_RESPONSE_RESULT_OK
(0).
The query results are stored in a String ArrayList with key
DETAILS_LIST
. The purchase information is stored in the String in JSON format. To see the types of product detail information that are returned, see In-app Billing Reference.In this example, you are retrieving the prices for your in-app items from the skuDetails
Bundle
returned from the previous code snippet.int response = skuDetails.getInt("RESPONSE_CODE"); if (response == 0) { ArrayList responseList = skuDetails.getStringArrayList("DETAILS_LIST"); for (String thisResponse : responseList) { JSONObject object = new JSONObject(thisResponse); String sku = object.getString("productId"); String price = object.getString("price"); if (sku.equals("premiumUpgrade")) mPremiumUpgradePrice = price; else if (sku.equals("gas")) mGasPrice = price; } }