在应用程序计费错误android为sku

时间:2014-04-11 05:50:27

标签: android in-app-purchase in-app-billing

Stub.getBuyIntent: Input Error: Non empty/null argument expected for sku

IabHelper(12228): In-app billing error: Unable to buy item, Error response: 5:Developer Error

有人可以解释Android内部应用内购买中的这个奇怪错误。

public class Purchase extends Activity {

    IInAppBillingService mService;
    private Bundle extras;
    private int add=0;//add coins to the kitty
    private int buying_choice;
    private String ITEM_SKU_SELECTION;
    private static final String TAG = "com.apps.guess.inappbilling";
    IabHelper mHelper;
    static final String ITEM_SKU = "android.test.purchased";
    private ServiceConnection mServiceConn;

    @Override 
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

        String base64EncodedPublicKey = "<64-bit> long";
            String buy2x_recd = UserData.getRestorePurchase(); 

        if(UserData.getBuying_choice()!=2) ITEM_SKU_SELECTION = selectbuyingChoice(UserData.getBuying_choice());
        if(buy2x_recd.equalsIgnoreCase("buy_2x"))callService();

        mHelper = new IabHelper(this, base64EncodedPublicKey);
        mHelper.startSetup(new 
                IabHelper.OnIabSetupFinishedListener() {
                      public void onIabSetupFinished(IabResult result) 
                  {
                        if (!result.isSuccess()) {
            Toast.makeText(getApplicationContext(), "In-app Billing setup failed: = "+UserData.getPack_level(), Toast.LENGTH_LONG).show();
                        Log.d(TAG, "In-app Billing setup failed: " + result);
                      } else {             
                            Log.d(TAG, "In-app Billing is set up OK");
               mHelper.launchPurchaseFlow(Purchase.this, ITEM_SKU_SELECTION,10001,mPurchaseFinishedListener, "");
                            mHelper.enableDebugLogging(true, TAG);
                  }
                   }
                });

    }//onCreate() ends

    public void callService(){
        mServiceConn = new ServiceConnection() 
        {
        @Override
            public void onServiceConnected(ComponentName name,
                    IBinder service) {
                  mService = IInAppBillingService.Stub.asInterface(service);
                    Log.d("TEST", "mService ready to go!");
                    checkownedItems();  
        }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                 mService = null;
            }
        };

        bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);
        }

     private void checkownedItems()
        {

    try {
        Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);

        int response = ownedItems.getInt("RESPONSE_CODE");
        if (response == 0) {
           ArrayList<String> ownedSkus =
              ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
           ArrayList<String>  purchaseDataList =
              ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
           ArrayList<String>  signatureList =
              ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE");
           String continuationToken = 
              ownedItems.getString("INAPP_CONTINUATION_TOKEN");

           for (int i = 0; i < purchaseDataList.size(); ++i) {
              String purchaseData = purchaseDataList.get(i);
              String signature = signatureList.get(i);
              String sku = ownedSkus.get(i);

              if(sku.equalsIgnoreCase("buy_2x"))
              {

                AlertDialog.Builder builder = new AlertDialog.Builder(Purchase.this);
                builder.setTitle("Purchased Already");
                builder.setMessage("You have already purchased the 2X option. Do you want to enable it?");

                builder.setPositiveButton("Yes!", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog1, int which) {
                        Toast.makeText(getApplicationContext(), "You have decided to double your winnings!.Please wait...", Toast.LENGTH_LONG).show();

                        int double_2x_value=2*getResources().getIntArray(R.array.start_parameters)[2];
                        UserData.writeData(""+double_2x_value, Purchase.this, UserData.get2XFile());
                        UserData.setWinningValue(""+double_2x_value);

                        UserData.writeData("true", Purchase.this, UserData.getDoubleXPurchaseFile());
                        UserData.setBuy2X(true);

                        UserData.setBuying_choice(0);
                        dialog1.dismiss();

                         Intent intent = new Intent(Purchase.this,GuessTheWordMainActivity.class);
                         startActivity(intent);

                        }});

                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog1, int which) {
                        dialog1.dismiss();
                        }});
                AlertDialog alert = builder.create();
                alert.show();
                  break;
              }

           } 
    }
            else {
            Toast.makeText(Purchase.this, "No 2X purchase available on your device",Toast.LENGTH_LONG).show();
            ITEM_SKU_SELECTION=selectbuyingChoice(2);
        } 
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

    private String selectbuyingChoice(int buying_choice2) {
        String ITEM_SKU_SELECTION=null;
        switch(buying_choice2){
        case 0:
            ITEM_SKU_SELECTION = "buy_200_coins";
            add = 200;
            Toast.makeText(getApplicationContext(), "You have decided to purchase 200 coins.Please wait...", Toast.LENGTH_LONG).show();
            break;

        case 1:
            ITEM_SKU_SELECTION = "buy_1200_coins";
            add = 1200;
            Toast.makeText(getApplicationContext(), "You have decided to purchase 1200 Coins.Please wait...", Toast.LENGTH_LONG).show();
            break;

        case 2:
            ITEM_SKU_SELECTION = "buy_2x";
            add = 0;
            Toast.makeText(getApplicationContext(), "You have decided to double your winnings!.Please wait...", Toast.LENGTH_LONG).show();
            break;

        default:
            Toast.makeText(getApplicationContext(), "NO BUYING CHOICE", Toast.LENGTH_SHORT).show();
            break;

        }

        return ITEM_SKU_SELECTION;

    }

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
              new IabHelper.OnConsumeFinishedListener() {
               public void onConsumeFinished(Purchase purchase, 
                     IabResult result) {

             if (result.isSuccess()) {               


                 int total =add+Integer.parseInt(UserData.getCoins_remaining());

                 if(!(ITEM_SKU_SELECTION.equalsIgnoreCase("buy_2x")))
                 Toast.makeText(getApplicationContext(), "Successful purchase done of "+add+"  Coins!", Toast.LENGTH_LONG).show();


                 if(ITEM_SKU_SELECTION.equalsIgnoreCase("buy_2x")){


                     UserData.writeData(""+double_2x_value, Purchase.this, UserData.get2XFile());
                     UserData.setWinningValue(""+double_2x_value);

                     UserData.writeData("true", Purchase.this, UserData.getDoubleXPurchaseFile());
                     UserData.setBuy2X(true);
                 }

                 add=0;
                 Intent intent = new Intent(Purchase.this, Guess.class);
                 startActivity(intent);

             } else {
                 Toast.makeText(getApplicationContext(), "NO PURCHASE DONE", Toast.LENGTH_LONG).show();
                 Intent intent = new Intent(Purchase.this, com.glambleapps.guesstheword.GuessTheWordMainActivity.class);
                 startActivity(intent);

             }
          }
        };

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHelper != null) mHelper.dispose();
        mHelper = null;

        if(mServiceConn != null)
        {
            unbindService(mServiceConn);
            mServiceConn=null;
        }

    }

    public void consumeItem() {
        mHelper.queryInventoryAsync(mReceivedInventoryListener);
    }


    IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener 
       = new IabHelper.QueryInventoryFinishedListener() {
           public void onQueryInventoryFinished(IabResult result,
              Inventory inventory) {


              if (result.isFailure()) {

                  Intent intent = new Intent(Purchase.this, Guess.class);
                  startActivity(intent);

              } else {
                     mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU_SELECTION), mConsumeFinishedListener);
              }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
          if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {     
            super.onActivityResult(requestCode, resultCode, data);
          }

          if (resultCode == RESULT_CANCELED) {
              Toast.makeText(Purchase.this,"Sorry, you have canceled your Guess-coins purchase.",Toast.LENGTH_SHORT).show();
          }
    }

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
    = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, 
                    Purchase purchase) 
    {
       if (result.isFailure()) {

             Intent intent = new Intent(Purchase.this, Guess.class);
             startActivity(intent);

          return;
     }      
     else if (purchase.getSku().equals(ITEM_SKU_SELECTION)) {consumeItem();
    }

   }
};

}//class ends 

1 个答案:

答案 0 :(得分:0)

就我而言,我收到此错误是因为我忘记激活应用内商品价格的状态

如果这不能解决您的问题,请调试getBuyIntent()方法并查看是否有任何传入其中的值(例如sku)为null或格式错误。