據谷歌實現應用內結算的文件,這就是谷歌將同步發送響應代碼的響應包映射到RESPONSE_CODE關鍵 In-app Billing Reference的Android應用內結算處理谷歌服務器響應代碼
現在是一個整數我有這樣的代碼在我的應用程序
@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
我tryed使這個
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();
}
}
}
}
}
日誌文件:它是給購買者成功和下載項目,但下次我嘗試buyint之前購買同樣的東西ENT開始,它的崩潰,給這個錯誤
這段代碼有什麼錯誤給下一次
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)
正是你想要什麼? –
如果項目已擁有,我想爲用戶下載而不再購買它,因爲他已經擁有它 – hasd11
我已經更新了我的答案嘗試並讓我知道。 –