androidcodeexamples.com
Offcode ltd logo
ANDROID
APACHE
THUMBNAILS
SHELL SCRIPTS
World-class examples brought to you by Offcode ltd
ANDROID
Developing the empire Bluetooth low energy Facebook API Google URL shortener Short URL lookup Tabs and fragments Privacy policy Front Page
androidcodeexamples.com

Android URL shortener: from short back to original long format

In this example the short format conversion to its original form via the Google URL shortener service is elaborated. There might be some security rules on what URLs may be converted back. It could be that just the ones you have shortened via your API key may be returned.

It actually contains also a simple URL cache to avoid unnecessary network traffic. Up to 30 entries are stored into the cache.

Please note that the apiKey is not correct here. It's been modified to a random value in purpose. You need to get yours from Google developer console.

package fi.offcode.selfad; 
 
/** 
 * Created by Eero on 12.5.2017. 
 */ 
 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.ProtocolException; 
import java.net.URL; 
import java.util.ArrayList; 
 
import org.json.JSONException; 
import org.json.JSONObject; 
 
class URL_Decoder { 
    private ArrayList<URL_Cache> urlsInCache = new ArrayList<>(); 
    private HttpURLConnection conn; 
    private URL url; 
    private String returnString; 
    private String resp; 
    private JSONObject jObject; 
    private static final String apiKey = "?key=AIzaSyAlhILOP5sqTblW9F_YXlzULssmUNV-6gQ"; 
    private static final String address = "https://www.googleapis.com/urlshortener/v1/url?shortUrl="; 
    private static final int MAX_CACHE_ENTRIES = 30; 
 
    URL_Decoder() { 
    } 
 
    private String readStream(InputStream is) throws IOException { 
        StringBuilder sb = new StringBuilder(); 
        BufferedReader r = new BufferedReader(new InputStreamReader(is), 1000); 
        for (String line = r.readLine(); line != null; line = r.readLine()) { 
            sb.append(line); 
        } 
        is.close(); 
        return sb.toString(); 
    } 
 
    String expand(String shortURL) { 
        if (shortURL == null) { 
            return null; 
        } 
 
        if (urlsInCache.size() > 0) { 
            for (URL_Cache item : urlsInCache) { 
                if (item != null) { 
                    if (item.getShortURL().equals(shortURL)) { 
                        return item.getLongURL(); 
                    } 
                } 
            } 
        } 
 
        try { 
            String google_url = address + shortURL + apiKey; 
            url = new URL(google_url); 
        } catch (IOException e) { 
            throw new RuntimeException(e); 
        } 
        try { 
            conn = (HttpURLConnection) url.openConnection(); 
        } catch (IOException e) { 
            return null; 
        } 
        conn.setDoOutput(false); 
 
        try { 
            conn.setRequestMethod("GET"); 
        } catch (ProtocolException e) { 
            throw new RuntimeException(e); 
        } 
 
        try { 
            InputStream inputStream = conn.getInputStream(); 
            resp = readStream(inputStream); 
        } catch (java.io.IOException e) { 
            conn.disconnect(); 
            return null; 
        } 
        try { 
            jObject = new JSONObject(resp); 
        } catch (JSONException e) { 
            e.printStackTrace(); 
            conn.disconnect(); 
            return null; 
        } 
        try { 
            returnString = jObject.getString("longUrl"); 
        } catch (JSONException e) { 
            e.printStackTrace(); 
            conn.disconnect(); 
            return null; 
        } 
 
        conn.disconnect(); 
 
        if (returnString != null) { 
            urlsInCache.add(new URL_Cache(returnString, shortURL)); 
            // Remove from head 
            if (urlsInCache.size() > MAX_CACHE_ENTRIES) { 
                urlsInCache.remove(0); 
            } 
        } 
        return returnString; 
    } 
}


URL Caching

The results (short format URLs) may be polled often. This of course depends on your application. However, it's always a good idea to avoid all unnecessary network traffic. For this reason this simple cache is built. After it's full, it starts to delete the oldest entries. That behavior is not in the cache itself but in the code above. This has no "most used" tags etc that could of course be even better.

package fi.offcode.selfad; 
 
/** 
 * Created by Eero on 12.6.2017. 
 */ 
 
public class URL_Cache { 
    private String shortURL; 
    private String longURL; 
 
    public URL_Cache(String lUrl, String sUrl) { 
        longURL = lUrl; 
        shortURL = sUrl; 
    } 
 
    public String getShortURL() { 
        return shortURL; 
    } 
 
    public String getLongURL() { 
        return longURL; 
    } 
} 

Written on April 2018