开发者

Problems with AndAppStore's Purchase Checking

开发者 https://www.devze.com 2023-01-15 14:11 出处:网络
I ha开发者_Go百科ve asked the same question of their support staff, but I thought I would try here, too.I have tried doing an HTTP Post with their sample data, and the response I get back doesn\'t mat

I ha开发者_Go百科ve asked the same question of their support staff, but I thought I would try here, too. I have tried doing an HTTP Post with their sample data, and the response I get back doesn't match what it's supposed to. Does anyone have sample code which includes the HTTP Post and conversion of the response to a byte array?

Here are the instructions from their site: http://andappstore.com/AndroidApplications/purchase_checking.jsp

Seems simple enough, but the byte array I get back has 39 elements when it is only supposed to have 20. I assume the problem is on my side, but I don't really know. I can post sample code if that helps.


I found the answer on their website in the section about their licensing functionality that I was able to adapt to Purchase Checking. Here is the working code, using their sample values. (hardcoded) Their sample code includes writing the buffer out to a file. I commented it out, but left it in here.

            boolean validated = false;
            byte[] Resp = AndAppStorePurchaseChecking("test@andappstore.com","98765", "543788");
            if(isValidPurchase(Resp))
                validated = true;


       private byte[] AndAppStorePurchaseChecking(String u, String d, String a) {

            final HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, "UTF-8");
            final SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(), 80));
            final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
            HttpClient httpClient = new DefaultHttpClient(manager, params);

           byte[] buffer = null; 
           final Uri.Builder uri = new Uri.Builder();
            uri.path("/AndroidApplications/purchaseCheck");

            uri.appendQueryParameter("u", u);
            uri.appendQueryParameter("d", d);
            uri.appendQueryParameter("a", a);                               

            HttpEntity entity = null;
            HttpHost host = new HttpHost("andappstore.com", 80, "http");
            try {
                final HttpResponse response = httpClient.execute(host, new HttpPost(uri.build().toString()));
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    entity = response.getEntity();
                    final InputStream in = entity.getContent();
                    //FileOutputStream fos = openFileOutput("AndLicense.001", MODE_PRIVATE);
                    try {
                        buffer = new byte[20];
                        in.read(buffer);
//                      int len;
//                      while((len = in.read(buffer)) > 0 ) {
//                          fos.write(buffer, 0, len);
//                      }
                    } finally {
                        //fos.close();
                    }
                }               
            } catch (Exception ex) {
                new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle("Error validating installation")
                    .setMessage(ex.getMessage())
                    .setPositiveButton("OK", null)
                    .show(); 
            } finally {
                if (entity != null) {
                    try {
                        entity.consumeContent(); 
                    } catch(IOException ioe) {
                        // Ignore errors during consumption, there is 
                        // no possible corrective action.
                    }
                }
            }  
            return buffer;
        }   


    public boolean isValidPurchase(final byte[] fromServer) {           
          if( fromServer == null || fromServer.length == 0 ) 
            return false;

          try{
              MessageDigest md = MessageDigest.getInstance("SHA1");
              byte[] digest = md.digest("98765PURCHASING-API-KEY".getBytes("UTF-8"));
              return Arrays.equals(fromServer, digest);
          } catch(Exception ex) {
              return false;
          }
        }   
0

精彩评论

暂无评论...
验证码 换一张
取 消