根据谷歌实施应用内结算文档,该谷歌将同步发送响应代码作为映射到响应包中的RESPONSE_CODE键的整数 In-app Billing Reference
现在我的应用程序中有这个代码
@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");
alert("You have bought the " + sku + ". Excellent choice,
adventurer!");
}
catch (JSONException e) {
alert("Failed to parse purchase data.");
e.printStackTrace();
}
}
}
}
我如何处理谷歌回复代码,例如
if (responsecode == 7) do somthing
我试着做这个
if(resultCode == RESULT_OK || resposeCode == 7) do somthing
和
if(resultCode == RESULT_OK || resultCode == 7) do somthing
但我当然在这里,因为没有什么对我有用
更新:
public class Test extends Activity {
IInAppBillingService mService;
ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
};
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Intent serviceIntent =
new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button3);
b1.setTypeface(font);
b2.setTypeface(font);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buy(id);
}}
@Override
public void onDestroy() {
super.onDestroy();
if (mService != null) {
unbindService(mServiceConn);
}
}
public void buy(String id) {
try {
Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), selected, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
this.startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
} catch (RemoteException e) {
e.printStackTrace();
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
@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");
Toast.makeText(getApplication(), "request true", Toast.LENGTH_LONG).show();
if (resultCode == RESULT_OK) {
Toast.makeText(getApplication(), "result true ", Toast.LENGTH_LONG).show();
if (responseCode == 0){
Toast.makeText(getApplication(), " response = 0", Toast.LENGTH_LONG).show();
}
else if(responseCode == 7){
Toast.makeText(getApplication(), "response = 7 ", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplication(), "error", Toast.LENGTH_LONG).show();
}
}
}
}
}
logfile:它给买家成功并下载了项目但是下次我尝试在buyintent开始之前买同样的东西,它会崩溃并发出此错误
这段代码下次会给出错误
this.startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
java.lang.NullPointerException
at com.app.inapp.bill.Test.buy(Test.java:258)
at com.app.inapp.bill.Test$3.onClick(Test.java:226)
at android.view.View.performClick(View.java:4651)
at android.view.View$PerformClick.run(View.java:19310)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
好的thx Sree Reddy Menon我找到了解决办法:
看起来谷歌不允许在你已经拥有物品并且给予nullpointerexption时启动buyintent,所以我使用getPurchases()来查找用户是否拥有该物品,并且只有在用户不知道的情况下才会调用购买意图&#39 ; t有项目
任何人都有同样问题的例子,这是我现在购买的方法
public void donate(String selected) {
try {
Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), selected, "inapp", "bGoa+Vlc/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
if(pendingIntent != null) {
this.startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
}else{
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_LIST");
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.equals(selected)){
Toast.makeText(getApplication(), "user have this item", Toast.LENGTH_LONG).show();
break;
}
}
// if continuationToken != null, call getPurchases again
// and pass in the token to retrieve more items
}}catch (RemoteException e)
{
e.printStackTrace();
}
}
} catch (RemoteException e) {
e.printStackTrace();
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}}
答案 1 :(得分:0)
// Billing response codes
public static final int BILLING_RESPONSE_RESULT_OK = 0;
public static final int BILLING_RESPONSE_RESULT_USER_CANCELED = 1;
public static final int BILLING_RESPONSE_RESULT_SERVICE_UNAVAILABLE = 2;
public static final int BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE = 3;
public static final int BILLING_RESPONSE_RESULT_ITEM_UNAVAILABLE = 4;
public static final int BILLING_RESPONSE_RESULT_DEVELOPER_ERROR = 5;
public static final int BILLING_RESPONSE_RESULT_ERROR = 6;
public static final int BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED = 7;
public static final int BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED = 8;
处理回复
@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 (responseCode == BILLING_RESPONSE_RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData);
String sku = jo.getString("productId");
alert("You have bought the " + sku + ". Excellent choice,
adventurer!");
}
catch (JSONException e) {
alert("Failed to parse purchase data.");
e.printStackTrace();
}
} else if (responseCode == BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) {
alert("You have already bought the item");
} else if (resultCode == Activity.RESULT_CANCELED) {
alert("Purchase canceled ");
}
else{
alert("Unknown error");
}
}
}
在if else上添加您的回复代码。