2016-03-07 38 views
0

据谷歌实现应用内结算的文件,这就是谷歌将同步发送响应代码的响应包映射到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) 
+0

正是你想要什么? –

+0

如果项目已拥有,我想为用户下载而不再购买它,因为他已经拥有它 – hasd11

+0

我已经更新了我的答案尝试并让我知道。 –

回答

1

确定THX SREE雷迪梅农我发现我的解决方案:

看起来像谷歌不允许启动buyintent当你已经有了项目,并给予nullpointerexption,所以我用getPurchases()找到,如果用户拥有的项目或无,并呼吁购买意向只有当用户没有项目

例如,对于任何人有同样的问题,这是我买的方法,现在

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(); 
    } 
}} 
+0

但不要从MainThread调用.getPurchases。请使用IABHelper类。并观看此视频@ 21.20 https://www.youtube.com/watch?v=DgcJPIRpfSk –

+0

谢谢我会尝试学习IABHelper – hasd11

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"); 

} 
} 

     } 

上,如果其他人添加为响应代码。

+0

很抱歉没有更新 – hasd11

+0

!现在试试 –

+0

没什么新意,可惜没有工作 – hasd11

相关问题