6
7

More than 5 years have passed since last update.

AndroidでGoogleのURL短縮サービスを使う時に気を付けること

Posted at

AndroidでGoogleのURL短縮サービス(goo.gl)を使いたいときがあったら

ぜんてい

  • goo.gl の短縮でOK(URL短縮サービスは他にもあります)
  • 個人アカウントに短縮履歴は残さない(個人認証=>URL短縮 という感じで使うと履歴に残ります)
  • Google のアカウントがあって、API Keyを取得済み

じっそう

Android固有の実装はしないので、Java関連で探せばネット上に転がっているかもしれません

注意点

  • メインスレッドで実行すると例外をはきますので、必ず別スレッドで実行するように実装する
UrlshortenerRequestInitializer initializer = new UrlshortenerRequestInitializer("Replace your API key");
Urlshortener service =
  new Urlshortener.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), null)
  .setUrlshortenerRequestInitializer(initializer)
  .build();
Url toInsert = new Url().setLongUrl("http://qiita.com");
try {
    Url result = service.url().insert(toInsert).execute();
    Log.d(LOG_TAG, result.getId());// getId() で短縮URLを取得します
} catch (Exception e) {
    Log.e(LOG_TAG, "error during url shortner", e);
}

けつろん

  • AndroidでGoogleAPIを使うときは認証系がやっかい
  • Expression Builder パターンは使いやすいね
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7