2016-03-07 70 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