在与试图导入库自己多摆弄,我终于找到了我可以做到这一点使用谷歌Eclipse插件,的这里。
不过,我似乎无法找到如何真正使用Android上是编译,因为在这些例子所需的类似乎不能够解析Eclipse中的API,至少没有任何的例子,所以我只能假设这些类并不在由谷歌插件的URL缩短API进口Eclipse中的图书馆存在。最接近的例子我能找到的是此处,这似乎是谷歌应用程序引擎,而不是机器人,并使用类,我似乎无法获得访问。
所以,问题是,我该如何使用这个API来获取URL的一个缩短的版本,在Android应用程序? preferably,我想,而不是使用OAuth的API密钥来做到这一点。
解决方案
谷歌API的客户端1.17.0-rc.jar
谷歌API的客户端的android-1.17.0-rc.jar
谷歌API的服务,urlshortener-V1-rev22-1.17.0-rc.jar
谷歌HTTP客户端-1.17.0-rc.jar
谷歌HTTP客户端,Android为1.17.0-rc.jar
方法:
字符串缩短(字符串longUrl){ Urlshortener.Builder建设者=新Urlshortener.Builder(AndroidHttp.newCompatibleTransport(),AndroidJsonFactory.getDefaultInstance(),NULL); Urlshortener urlshortener = builder.build(); com.google.api.services.urlshortener.model.Url URL =新的URL(); url.setLongUrl(longUrl); 尝试 { 。URL = urlshortener.url()插入(URL).execute(); 返回url.getId(); }赶上(IOException异常E){ 返回null; } }
After much fiddling with trying to import the libraries myself, I finally managed to find out that I can do so using the Google Plugin for Eclipse, here.
However, I seem to be unable to find any examples of how to actually use the API on Android, at least none that are compilable, as the classes required in those examples seem to not be resolvable by Eclipse, so I can only assume that these classes do not exist in the libraries that are imported by the Google Plugin for Eclipse for the URL Shortener API. The closest thing to an example I could find is here, which appears to be for Google App Engine, not Android, and uses classes that I cannot seem to get access to.
So the question is, how do I use this API to get a shortened version of a URL, in an Android application? Preferably, I would like to do it using an API Key, instead of OAuth.
解决方案<meta-data android:name="com.google.android.urlshortener.API_KEY" android:value="{YOUR_API_KEY}"/>
google-api-client-1.17.0-rc.jar
google-api-client-android-1.17.0-rc.jar
google-api-services-urlshortener-v1-rev22-1.17.0-rc.jar
google-http-client-1.17.0-rc.jar
google-http-client-android-1.17.0-rc.jar
method:
String shorten(String longUrl){ Urlshortener.Builder builder = new Urlshortener.Builder (AndroidHttp.newCompatibleTransport(), AndroidJsonFactory.getDefaultInstance(), null); Urlshortener urlshortener = builder.build(); com.google.api.services.urlshortener.model.Url url = new Url(); url.setLongUrl(longUrl); try { url = urlshortener.url().insert(url).execute(); return url.getId(); } catch (IOException e) { return null; } }
更多推荐
如何使用Android上的谷歌URL缩短API?
发布评论