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 long format to short

In this example the Google URL shortener long to short -format converting process is elaborated. This means that by sending a long url (whatever it may be) the Google service returns a shortened URL. Second the last line strips out the https:// prefix as the URL is used in the Eddystone URL BLE broadcast. Other than that, the code should be quite self-explanatory. This example has been used successully on multiple Android devices with various API levels.

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.OutputStream; 
import java.io.InputStreamReader; 
 
import java.net.HttpURLConnection; 
import java.net.ProtocolException; 
import java.net.URL; 
import org.json.JSONException; 
import org.json.JSONObject; 
 
import android.util.Log; 
 
class URL_Shortener { 
    private JSONObject reqObj; 
    private HttpURLConnection conn; 
    private String returnString; 
    private String resp; 
    private URL url; 
    private JSONObject jObject; 
    private static final String TAG = "url_short"; 
    private static final String apiKey = "?key=AIzaSyAlhILOP5sqTblW9F_YXlzULssmUNV-6gQ"; 
    private static final String address= "https://www.googleapis.com/urlshortener/v1/url"; 
 
    URL_Shortener(){ 
    } 
 
    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 shorten(String longURL) { 
        if (longURL == null) { 
            Log.d(TAG, "Long url is NULL!"); 
            return null; 
        } 
        JSONObject reqObj = new JSONObject(); 
 
        try { 
            reqObj.put("longUrl", longURL); 
        } catch (JSONException e) { 
            e.printStackTrace(); 
        } 
 
        try { 
            String google_url = address + apiKey; 
            url = new URL(google_url); 
        } catch (IOException e) { 
            throw new RuntimeException(e); 
        } 
        try { 
            conn = (HttpURLConnection) url.openConnection(); 
        } catch (IOException e) { 
            return null; 
        } 
        conn.setRequestProperty("Content-Type", "application/json"); 
        try { 
            conn.setRequestMethod("POST"); 
        } catch (ProtocolException e) { 
            conn.disconnect(); 
            return null; 
        } 
        conn.setDoOutput(true); 
 
        try { 
            OutputStream outputStream = conn.getOutputStream(); 
            outputStream.write(reqObj.toString().getBytes()); 
        } catch (java.io.IOException e) { 
            conn.disconnect(); 
            return null; 
        } 
        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(); 
        } 
        try { 
            returnString = jObject.getString("id"); 
        } catch (JSONException e) { 
            e.printStackTrace(); 
            conn.disconnect(); 
            return null; 
        } 
 
        conn.disconnect(); 
 
        String rtString = returnString.replace("https://",""); 
 
        return rtString; 
    } 
}


Written on April 2018