繁体   English   中英

应用内购买错误您已拥有此商品Android

[英]In-app purchase Error you already own this item Android

帮我!!!

我是Android初学者,目前正在测试应用。 该应用只有2个按钮,当您按“购买测试项目”按钮时,它会进行应用内购买。 而且,当您按下使用按钮时,它将运行消耗购买功能。

这是我的代码:

package com.ovation.testpmt;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.vending.billing.IInAppBillingService;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;`enter code here`
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity implements OnClickListener{


    IInAppBillingService mservice;
    ServiceConnection connection;

    //String inappid = "android.test.purchased"; //replace this with your in-app product id
    String inappid ="234";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mservice = IInAppBillingService.Stub.asInterface(service);  
                try {
                    mservice.getPurchases(3, getPackageName(), "inapp", null);
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub
                mservice = null;
            }
        };

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

        Button purchaseBtn = (Button) findViewById(R.id.purchase);
        Button consumeBtn = (Button) findViewById(R.id.consume); 

        purchaseBtn.setOnClickListener(this);       
        consumeBtn.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (requestCode == 1001) {           
              int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
              String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
              String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");

              if (resultCode == RESULT_OK) {
                 try {
                    JSONObject jo = new JSONObject(purchaseData);
                    String sku = jo.getString("productId");
                    Log.i("test","You have bought the " + sku + ". Excellent choice, adventurer!");
                  }
                  catch (JSONException e) {
                     Log.i("test","Failed to parse purchase data.");
                     e.printStackTrace();
                  }
              }
           }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (connection != null) {
            unbindService(connection);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        // TODO Auto-generated method stub
        if(v.getId()==R.id.purchase){
            ArrayList<String> skuList = new ArrayList<String>();
            skuList.add(inappid);
            Bundle querySkus = new Bundle();
            querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
            Bundle skuDetails;
            try {
                skuDetails = mservice.getSkuDetails(3, getPackageName(),
                        "inapp", querySkus);

                int response = skuDetails.getInt("RESPONSE_CODE");

                if (response == 0) {

                    ArrayList<String> 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(inappid)) {

                            Bundle buyIntentBundle = mservice
                                    .getBuyIntent(3, getPackageName(), sku,
                                            "inapp",
                                            "aaa");

                            PendingIntent pendingIntent = buyIntentBundle
                                    .getParcelable("BUY_INTENT");
                            if(buyIntentBundle==null){
                                Log.i("test", "Your everything is empty123");
                            }else{
                                if(pendingIntent == null){


                                }else{

                                    startIntentSenderForResult(
                                            pendingIntent.getIntentSender(), 1001,
                                            new Intent(), Integer.valueOf(0),
                                            Integer.valueOf(0), Integer.valueOf(0));

                                }

                            }   

                        }
                    }
                }
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SendIntentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else if(v.getId()==R.id.consume){
            Bundle ownedItem = null;
            try {
                ownedItem = mservice.getPurchases(3, getPackageName(), "inapp", null);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int res = ownedItem.getInt("RESPONSE_CODE");
            Log.i("test", "response ---> "+res);
            if(res==0){
                Log.i("test", "res is 0");
                ArrayList<String> ownedSkus = 
                          ownedItem.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");

                Log.i("test", "the size of ownedItem is "+ownedSkus.size());

                for(String str:ownedSkus){
                    Log.i("test", "Str ---> "+str);
                }
                 ArrayList<String> purchaseDataList = 
                         ownedItem.getStringArrayList("INAPP_PURCHASE_DATA_LIST");

                 for(String str:purchaseDataList){
                        Log.i("test", "Str ---> "+str);
                    }
            }

            String token = "my actual string token";
            try {
                int Buyresponse = mservice.consumePurchase(3, getPackageName(), token);


            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
}

我在开发人员控制台上拥有应用程序内产品,并且在清单文件中授予了权限。

第一次,它运行得很好,但是当我尝试通过按“使用按钮”来删除购买并再次运行时,它显示了“错误,您已经拥有此商品”消息。

谁能给我解决这个问题的方法吗?

因为没有消耗产品,所以您遇到了这个问题。 因为您只能在消费后的另一时间购买产品。

请检查您的消费按钮代码。

我也建议您使用最新的inAppBilling API

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM