I have some problems with in app billing, I wanted to create an product inside the app that can be bought several times. But google made the in app billing in a way that a product first must be consumed before you can buy it again. I tried consuming it with the following code:
if (resultCode == RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData1);
String sku = jo.getString(inappid);
Toast.makeText(
ABActivity.this,
"Purchase Successful",
Toast.LENGTH_LONG).show();
Bundle ownedItems = mservice.getPurchases(3, getPackageName(), "inapp", null);
// Check response
int responseCode = ownedItems.getInt("RESPONSE_CODE");
if (responseCode != 0) {
throw new Exception("Error");
}
// Get the list of purchased items
ArrayList<String> purchaseDataList =
ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
for (String purchaseData : purchaseDataList) {
JSONObject o = new JSONObject(purchaseData);
String purchaseToken = o.optString("token", o.optString("purchaseToken"));
// Consume purchaseToken, handling any errors
mservice.consumePurchase(3, getPackageName(), purchaseToken);
}
} catch (JSONException e) {
System.out.println("Failed to parse purchase data.");
e.printStackTrace();
} catch (RemoteException e) {
System.out.println("Failed to parse purchase data.");
e.printStackTrace();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {System.out.println("Failed to parse purchase data.");
e.printStackTrace();
// TODO Auto-generated catch block
e.printStackTrace();
}
The first time I try to buy the item it works perfectly but when I try to buy it again the app crashes. When I try to use the app with a virtual device and log the error with logcat it crashes the first time I try to buy the item and it gives an NullPointerException while the first time on my mobile phone it works perfect, so I don't think the NullPointerException is the problem.
Thanks in advance.