I'm attempting to take a shortened url and expand it out to its original full length url in a string format in java. I've been able to track down a tutorial online, however I'm unable to get this to actually get me the full url. Has anyone done this before or know how to do this? Any help is huge, thanks
URLConnection conn = null;
try {
URL inputURL = new URL("http://bit.ly/9mglq8");
conn = inputURL.openConnection();
} catch (MalformedURLException e) {
} catch (IOException ioe) {
}
String realU = conn.toString();
Toast.makeText(ImagetestActivity.this, realU,
Toast.LENGTH_LONG).show();
Before You Click, Reveal Full URLs There are a number of ways you can reveal the full URL behind a shortened URL: Use the shortening service preview feature. Type the shortened URL in the address bar of your web browser and add the characters described below to see a preview of the full URL: tinyurl.com.
One Simple Solution could be Hashing. Use a hash function to convert long string to short string. In hashing, that may be collisions (2 long URLs map to same short URL) and we need a unique short URL for every long URL so that we can access long URL back.
System.out.println("Short URL: "+ shortURL);
urlConn = connectURL(shortURL);
urlConn.getHeaderFields();
System.out.println("Original URL: "+ urlConn.getURL());
/* connectURL - This function will take a valid url and return a
URL object representing the url address. */
URLConnection connectURL(String strURL) {
URLConnection conn =null;
try {
URL inputURL = new URL(strURL);
conn = inputURL.openConnection();
int test = 0;
}catch(MalformedURLException e) {
System.out.println("Please input a valid URL");
}catch(IOException ioe) {
System.out.println("Can not connect to the URL");
}
return conn;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With