Shorten Url using Goo.gl
here json data send in http header as post request.
send variable by using redirect url method in longUrl
{'longUrl': 'https://www.google.com'}
this loginUrl is send as post request in http header and google.com is finally created as a shorten url.
public void sendPostBody() throws Exception {
String url = "https://www.googleapis.com/urlshortener/v1/url?key=YOUR_GENERATED_KEY"; //Generated your own key on your google account
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
// add header
post.setHeader("Content-type","application/json");
post.setEntity(new StringEntity("{'longUrl': 'https://www.google.com'}", "UTF8"));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(" The shorten Goo.gl Url is :::: "+result.toString());
}
This result contains a json in format like this:
{ "kind": "urlshortener#url", "id": "YourShorternURL", "longUrl": "https://www.google.com"}
Your Shorten URL is the "id" parameter in the JSON.
Source: http://adityagoyal-java.blogspot.in/2016/09/shorten-url-using-googl-by-http-post.html