Working with Google in-app billing?

独自空忆成欢 提交于 2019-12-11 19:06:03

问题


I'm trying to use Google in app billing in my app. My code dose not have any error in eclipse but when I run it on my phone it force closes! and log cat points error to "new Thread" line. what is the problem? I'll be thankful if someone could help.

Here is my code:

in onCreate under donate button:

@Override
            public void onClick(View v) {

                new Thread(new Runnable() {
                    public void run() {
                        ArrayList<String> skuList = new ArrayList<String>();
                        skuList.add("My Selling Obj ID");
                        Bundle querySkus = new Bundle();
                        querySkus.putStringArrayList("My App’s RSA Key"
                                , 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("My Selling Obj ID"))
                                      pool = price;
                               }
                            }

                        } catch (RemoteException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }




                        Bundle buyIntentBundle;
                        try {
                            buyIntentBundle = mService.getBuyIntent(3, getPackageName(),
                                       "My Selling Obj ID", "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
                            PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");

                            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 (SendIntentException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                    }
                  }).start();
            }

in onActivityResult:

@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");
                Toast.makeText(Main.this, "thanks for Donation!", Toast.LENGTH_SHORT).show();
                Toast.makeText(Main.this, pool, 
                isdonated = 1;
              }
              catch (JSONException e) {
                 //alert("Failed to parse purchase data.");
                 Toast.makeText(Main.this, "Donation Failed!", Toast.LENGTH_LONG).show();
                 e.printStackTrace();
              }
          }
       }
    }

here is logcat errors: http://upload7.ir/imgs/2014-02/08566691322028747175.jpg


回答1:


Have you added this permission to you manifest file?

<uses-permission android:name="com.android.vending.BILLING"/>


来源:https://stackoverflow.com/questions/21569487/working-with-google-in-app-billing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!