以下、ソースコード貼ります。
/**
* 短縮URL生成
* @param longUrl
* @return
* @throws ClientProtocolException
* @throws IOException
*/
@SuppressWarnings("resource")
private static String getShortUrl(String longUrl) throws ClientProtocolException, IOException {
HttpPost post = new HttpPost("https://www.googleapis.com/urlshortener/v1/url");
post.setHeader("Content-Type", "application/json");
post.setEntity(new StringEntity("{'longUrl': '"+longUrl+"'}", "UTF-8"));
HttpResponse response = new DefaultHttpClient().execute(post);
String responseText = EntityUtils.toString(response.getEntity());
// JsonFactoryの生成
JsonFactory factory = new JsonFactory();
// JsonParserの取得
@SuppressWarnings("deprecation")
JsonParser parser = factory.createJsonParser(responseText);
//JSONのパース処理
String shotUrl = "";
while (parser.nextToken() != JsonToken.END_OBJECT) {
String name = parser.getCurrentName();
if (name != null) {
parser.nextToken();
if (name.equals("id")) {
shotUrl = parser.getText();
}
}
}
return shotUrl;
}