如何使用Android上的谷歌URL缩短API?

编程入门 行业动态 更新时间:2024-10-24 06:37:27
本文介绍了如何使用Android上的谷歌URL缩短API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

不过,我似乎无法找到如何真正使用Android上是编译,因为在这些例子所需的类似乎不能够解析Eclipse中的API,至少没有任何的例子,所以我只能假设这些类并不在由谷歌插件的URL缩短API进口Eclipse中的图书馆存在。最接近的例子我能找到的是此处,这似乎是谷歌应用程序引擎,而不是机器人,并使用类,我似乎无法获得访问。

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

解决方案
  • 添加到您的清单中应用程序节点:
  • <元数据          机器人:名称=com.google.android.urlshortener.API_KEY          机器人:值={YOUR_API_KEY}/>

  • 添加如下因素库:
  •   

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

    解决方案

  • add to your manifest in application node:
  • <meta-data android:name="com.google.android.urlshortener.API_KEY" android:value="{YOUR_API_KEY}"/>

  • 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

  • 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?

    本文发布于:2023-11-30 01:18:28,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1648234.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何使用   Android   API   URL

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!