我问他们的支持人员问题,但我想我也会在这里试试。我尝试用他们的样本数据做一个HTTP Post,我得到的响应与它应该的不一致。有没有人有示例代码,其中包括HTTP Post和将响应转换为字节数组?
以下是其网站的说明: http://andappstore.com/AndroidApplications/purchase_checking.jsp
看起来很简单,但我得到的字节数组有39个元素,只有它应该有20个。我认为问题在我身边,但我真的不知道。如果有帮助,我可以发布示例代码。
答案 0 :(得分:0)
我在他们的网站上找到了关于他们的许可功能的部分的答案,我能够适应购买检查。这是使用其样本值的工作代码。 (硬编码)他们的示例代码包括将缓冲区写入文件。我评论了它,但把它留在了这里。
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;
}
}