应用内计费-应用内购买错误您已经拥有此项目Android



帮帮我!!!

我是安卓系统的初学者,目前正在做一个测试应用程序。这个应用程序只有2个按钮,当你按下"购买测试项目"按钮时,它会进行应用内购买。当你按下使用按钮时,它会运行consumerPurchase功能。

这是我的代码:

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的

相关内容

最新更新