再次写这个是非常郁闷的,主要怪我,live writter 2011还不支持,辛辛苦苦写了半天,不小心按住了刷新键,结果写的东西全没了,虽然很气愤,虽然手已经冰凉,还是坚持又整理了一遍,也希望51CTO以后能随时保存草稿,这是一个非常严重的问题:
如果你已经在Android Market上发布了应用程序,怎么通过编程方式访问Android Market呢?谷歌大神已经为我们提供好了接口-An open-source API for the Android Market,不仅Android程序可以访问, Ruby 和PHP也提供了借口,更多信息请参考:http://code.google.com/p/android-market-api/
OK,废话少说,That‘s it!
Current progress
- You can browse market with any carrier or locale you want.
- Search for apps using keywords or package name.
- Retrieve an app info using an app ID.
- Retrieve comments using an app ID.
- Get PNG screenshots and icon
Requirement
- *(1) A Google Account
- *(2) Include androidmarketapi-X.Y.jar and protobuf-java-X.Y.Z.jar in your classpath,
下载地址:http://code.google.com/p/android-market-api/downloads/list
要把这两个JAR导入到项目中去.由于代码比较好理解,就不添加注释了:-)
HowToSearchApps:
You can search by using :
By package name;
- String query = "pname:<package>";
By developper name :
- String query = "pub:<name>";
通过包名查看程序关键代码:
- String query = "pname:com.luckyxmobile.timers4me";// 通过包名查找程序
- MarketSession session = new MarketSession();
- session.login("your gmail account", "your password");
- AppsRequest appsRequest = AppsRequest.newBuilder().setQuery(query)
- .setStartIndex(0).setEntriesCount(10).setWithExtendedInfo(true).build();
- session.append(appsRequest, new MarketSession.Callback<AppsResponse>() {
- @Override
- public void onResult(ResponseContext context, AppsResponse response) {
- TextView text = (TextView) findViewById(R.id.text);
- String id = response.getApp(0).getId();
- String creatorID = response.getApp(0).getCreatorId();
- String creator = response.getApp(0).getCreator();
- String packageName = response.getApp(0).getPackageName();
- String price = response.getApp(0).getPrice();
- String rating = response.getApp(0).getRating();
- int ratingCount = response.getApp(0).getRatingsCount();
- String title = response.getApp(0).getTitle();
- String version = response.getApp(0).getVersion();
- int versionCode = response.getApp(0).getVersionCode();
- int serializedSize = response.getApp(0).getSerializedSize();
- ExtendedInfo extendedInfo = response.getApp(0).getExtendedInfo();
- text.setText("id:" + id + "\nCreatorId:" + creatorID
- + "\nCreator:" + creator + "\nPackageName:"
- + packageName + "\nPrice:" + price + "\nrating:"
- + rating + "\nRatingCount:" + ratingCount + "\ntitle:"
- + title + "\nVersion:" + version + "\nversionCode:"
- + versionCode + "\nDownloadsCount:"
- + extendedInfo.getDownloadsCount()
- + "\nDownloadsCountText:"
- + extendedInfo.getDownloadsCountText()
- + "\nInstallSize:" + extendedInfo.getInstallSize()
- + "\nSerializedSize:" + serializedSize
- + "\nDecription:" + extendedInfo.getDescription()
- + "\nContactEmail:" + extendedInfo.getContactEmail()
- + "\nContactPhone:" + extendedInfo.getContactPhone()
- + "\nContactWebsite:"
- + extendedInfo.getContactWebsite());
- }
- });
- .flush();//发送并刷新
运行结果:
HowToGetAppComments :
- CommentsRequest commentsRequest = CommentsRequest.newBuilder().setAppId("7065399193137006744") .setStartIndex(0).setEntriesCount(10)build();
- session.append(commentsRequest, new Callback<CommentsResponse>() {
- @Override
- public void onResult(ResponseContext context, CommentsResponse response) {
- System.out.println("Response : " + response);
- // response.getComments(0).getAuthorName()
- // response.getComments(0).getCreationTime()
- // ...
- } });
- session.flush();
HowToGetAppScreenshot :
- GetImageRequest imgReq = GetImageRequest.newBuilder().setAppId("-7934792861962808905").setImageUsage(AppImageUsage.SCREENSHOT).setImageId("1").build();
- session.append(imgReq, new Callback<GetImageResponse>() {
- @Override
- public void onResult(ResponseContext context, GetImageResponse response) {
- try {
- FileOutputStream fos = new FileOutputStream("icon.png");
- fos.write(response.getImageData().toByteArray());
- fos.close();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- } });
- session.flush();
最后提醒一下哦,不要忘记在配置文件中加上访问Internet的权限,当然了,我只是抛砖引玉的作用,动手才能更加精彩,Good luck!
本文转自 breezy_yuan 51CTO博客,原文链接:http://blog.51cto.com/lbrant/431851,如需转载请自行联系原作者