如何使用Android上的谷歌URL缩短API? [英] How do I use the Google URL Shortener API on Android?

查看:3819
本文介绍了如何使用Android上的谷歌URL缩短API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与试图导入库自己多摆弄,我终于找到了我可以做到这一点使用谷歌Eclipse插件,的这里

不过,我似乎无法找到如何真正使用Android上是编译,因为在这些例子所需的类似乎不能够解析Eclipse中的API,至少没有任何的例子,所以我只能假设这些类并不在由谷歌插件的URL缩短API进口Eclipse中的图书馆存在。最接近的例子我能找到的是<一个href="https://$c$c.google.com/p/google-api-java-client/source/browse/urlshortener-robots-appengine-sample/src/main/java/com/google/api/client/sample/urlshortener/appengine/robots/UrlShortenerSample.java?repo=samples"相对=nofollow>此处,这似乎是谷歌应用程序引擎,而不是机器人,并使用类,我似乎无法获得访问。

所以,问题是,我该如何使用这个API来获取URL的一个缩短的版本,在Android应用程序? preferably,我想,而不是使用OAuth的API密钥来做到这一点。

解决方案
  1. 添加到您的清单中应用程序节点:

 &LT;元数据
         机器人:名称=com.google.android.urlshortener.API_KEY
         机器人:值={YOUR_API_KEY}/&GT;
 

<醇开始=2>
  • 添加如下因素库:
  •   

    谷歌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

    <醇开始=3>
  • 方法:

     字符串缩短(字符串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.

    解决方案

    1. add to your manifest in application node:

     <meta-data
             android:name="com.google.android.urlshortener.API_KEY"
             android:value="{YOUR_API_KEY}"/>
    

    1. add folowing libraries:

    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

    1. 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆