帮助我!!!
我是Android的首发,现在正在做一个测试应用。这个应用程序只有2个按钮,当你按下“购买测试项目”按钮,它会通过应用内购买。当你按下使用按钮时,它会运行consumePurchase功能。
这是我的代码:
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();
}
}
}
}
我的开发者控制台上有应用内商品,我在清单文件中提供了权限。
第一次,它运行得非常好,但是当我尝试通过按下“使用按钮”并再次运行它来删除购买时,它给了我“错误,你已经拥有这个项目”消息。
有人能为我解决这个问题吗?
答案 0 :(得分:0)
您遇到此问题是因为产品没有被消耗掉。因为您只能在消费后再次购买产品。
请检查您的消费按钮代码。
另外,我建议您使用最新的inAppBilling API